How to detect edit mode on iPhone UITableView

The setEditing:animated: examples didn't work for me (on iOS 6.1) to detect the state changes that occur when you enter and exit delete confirmation mode. It seems that setEditing:animated: is only called once, when the table view goes into edit mode, but not on state changes of the cells. After some debugger fun, I arrived at a method to detect the cell state change.

My use case is different from yours. I just wanted to hide the label when the delete button is showing so that the other cell content doesn't overlap it when the Delete button slides in. (I'm using UITableViewCellStyleValue2, the one with the blue label on the left and black label on the right.)

(In your UITableViewCell subclass)

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    [super willTransitionToState:state];
    if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
        // showing delete button
        [self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label
    }
}

- (void)didTransitionToState:(UITableViewCellStateMask)state {
    if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
        // not showing delete button
        [self.textLabel setAlpha:1.0f]; // <-- show the label
    }
}

Kendall 's answer works. I did it in following way.

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    NSLog(@"Can edit %d", tableView.editing);
    if (tableView.editing == 1) {
        [self.editButtonItem setTitle:EDIT_BUTTON_TITLE];
    }else {
        [self.editButtonItem setTitle:DONE_BUTTON_TITLE];
    }

    return YES;
}

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

Swift Use below code accordingly:

open var isEditing: Bool // default is NO. setting is not animated.

open func setEditing(_ editing: Bool, animated: Bool)

When subclassing a tableviewcontroller (what most people are going to be doing most of the time since you have to override it's delegate methods just to put data into it...) you can just override the setEditing:animated: method to grab editing state changes.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    NSLog(@"Editing %i", editing);
    [super setEditing:editing animated:animated];
}

That passes the state change along to the super class, but lets you jump in the middle and detect the change, or alter it if you wanted...