I was having a tough time getting my UITableView to get into “Edit Mode” or editing style, as Apple calls it, when I touch the Edit button on my navigation bar.  I solved it, but here’s a little back story first.  I had my view setup in Storyboard, only it was a UITableViewController object, which doesn’t give me much control over extra elements and subviews, so I wanted to swap that out for a real view and use subviews containing my UITableView.  This wasn’t too big a deal to do, just had to change a few things and setup a new view controller .h and .m files in Xcode.  Because I didn’t have  “self.tableView” inherited anymore by the previous UITAbleViewController (as that’s all built-in), I lost my delegates, outlets, datasource and edit button.  Easy enough to fix, just setup some new outlets and properties.
So I got my tableView outlet setup and is used on the new table view object. Â I setup my datasource to use my main View Controller. Â When you highlight your main view in Storyboard, at the Connections Inspector, I made sure I had Referencing Outlets setup as shown below. Â It was the dataSource and delegates that needed pointing to the tableView property. Â (drag from there to the view in Storyboard to connect)
Assuming you have all the outlets setup, the table should display the data. Â In my case it was working fine up to this point, except the edit mode. Â The tableView method for “commitEditingStyle” was not firing. Â Of course, I didn’t have a button to edit on my nav bar either. Â That was fairly easy, just add this in viewDidLoad:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
Above, I set my navigation controller (which my view is embedded in) to add a button at the left to be an Edit button. Â You could also set that on the right if you wanted. Â So , cool, I had an edit button on the nav bar. Â But, still no edit mode on my table view.
I knew my dataSource and delegates were working, because I had data showing from cellForRowAtIndexPath method. Â Adding NSLog entries to any of the delegate methods didn’t work. Â I even added :
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
None of them were working. Â Then I had a DUH moment. Â The button isn’t initiating the edit mode! Â I was blaming the wrong objects. :) Â I found out that there’s a method for the UIViewController for setEditing. Â Adding this method was all I needed:
– (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
NSLog(@”EDIT BUTTON WILL BEGIN EDITING”);
if (editing) {
[self.tableView setEditing:YES animated:YES];
} else {
[self.tableView setEditing:NO animated:YES];
}
}
That was it! Â Edit button just worked like before at that point. Â :)