开发者

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


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


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;
}


That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editing 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜