0

It would seem like there should be an API on UITableViewDelegate (or maybe even UITableViewDataSource) to let the delegate know when a UITableView changes editing state. But I can't find anything like that.

I suppose I could use KVO to detect it, but I don't want to unless I have to.

Am I missing something? Or is there really no API for this?

Greg Maletic
  • 6,225
  • 8
  • 54
  • 73

2 Answers2

1

Well, UIViewController defines - (void)setEditing:(BOOL)editing animated:(BOOL)animated which you can override in your subclass. Always call the superclass implementation, but that gives you an opportunity to modify the table (such as inserting/removing rows) when transitioning between states.

Daniel Thorpe
  • 3,911
  • 2
  • 28
  • 28
  • Wow, had no idea that the viewcontroller had an editing attribute like UITableView does. I had been setting the editing property directly on the table, but I suppose I should be doing this on the viewcontroller (though why, exactly, the viewcontroller has an editing mode eludes me...). This seems like an okay solution, but I'm a little disturbed by having my table's presentation controlled by a third class (the view controller) on top of the delegate and datasource. Seems like the latter two classes should be enough. But thanks! – Greg Maletic Nov 28 '11 at 18:12
  • Yeah, it's a bit odd I suppose, but it's partly because also the view controller has changes that get made (in say, the nav bar). So it's really the view controller which is changing to edit mode, and it's telling it's view (in this case a table view) to transition to editing mode. – Daniel Thorpe Nov 29 '11 at 11:45
1

From what I can understand from this page, the delegate method tableView:canEditRowAtIndexPath: is also called when the edit button is tapped.. Which means that you don't have to subclass the tableview. I haven't tried this myself, but I'm pretty sure this will do the trick. See the link for more details.

Update:

It is a bit messy to do it like this, as the method is called once for every cell. The correct way would be to use the method Daniel Thorpe mentions in his answer (setEditing:animated:). But I would just override the method in the UITableViewController being used (unless you have a subclass that you are working with).

By the way, here is another thread about the exact same thing here on SO: link.

Community
  • 1
  • 1
matsr
  • 4,302
  • 3
  • 21
  • 36
  • That is true, but it's called for every row in your table (or, at least, every row currently on display.) Which is a little messy when what you're trying to accomplish is altering the presentation of your table when entering and exiting edit mode...I just want to be notified once. But you're right, there is a way to make this work. – Greg Maletic Nov 28 '11 at 18:03
  • You are right, it is called once for every cell. It's probably a bit messy to do it like this. The correct way would be to use `setEditing:animated:`. I'll update my answer. – matsr Nov 28 '11 at 22:13