Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect edit mode on iPhone UITableView

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

like image 515
phil swenson Avatar asked Nov 21 '09 17:11

phil swenson


4 Answers

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)
like image 145
Stephen Darlington Avatar answered Nov 13 '22 04:11

Stephen Darlington


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

like image 35
ima747 Avatar answered Nov 13 '22 03:11

ima747


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
    }
}
like image 4
Alex Nauda Avatar answered Nov 13 '22 05:11

Alex Nauda


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;
}
like image 3
Saturn Avatar answered Nov 13 '22 04:11

Saturn