UITableView commitEditngStyle not working with edit button and subview delegate 1


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.  :)