views:

521

answers:

2

For my iphone app, I have an editable (for delete) table view. I'd like to be able to detect that the user has clicked the "Edit" button. See this image: http://grab.by/It0

From the docs, it looked like if I implemented :

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

then I could detect it (although from the name of the method, I wouldn't think that). This proved not to work.

Any ideas on detecting this? The reason I want to is I want to hook up a "Delete all" button in the upper left hand corner when in delete mode.

thanks

+1  A: 

It is probably not working as you expect because willBeginEditingRowAtIndexPath: is called before the editing starts.

If you want to check while in another method you need the editing property:

@property(nonatomic, getter=isEditing) BOOL editing

If you want to do something when the 'Edit' button is pressed you need to implement the setEditing method:

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated

Which you'll find in UIViewController. (Well, that's the most likely place; there are others.)

Stephen Darlington
A: 

That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editng mode is entered, to ask each cell if it can be edited:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

I don't think overriding setEditing:animated: makes sense, since you would have to subclass UITableView which is extra work and a class you need for no other reason, not to mention it would have to communicate the fact that editing had been turned on back to the controller.

One other option is to simply add the Edit button yourself - it's a built in UIBarButtonSystemItem, you can add it and then have it call your own method in which you do something specific then call setEditing:animated: on the UITableView itself.

The idea behind editing is that when editing is enabled, each cell is told to go to edit mode, and as asked if there are any specific editing controls that should be applied. So in theory there's no need to detect entry into editing mode beyond changing the appearance of cells. What are you trying to do when editing mode is entered?

Kendall Helmstetter Gelner
setEditing is on the UITableViewController, which you probably already have subclassed, not on the tableview itself. As for a reason you might want to do this, lets say the user can reorder the rows, but you don't want to commit this reorder to the web server until it is all done.
Nathan Reed