UITableView in edit mode - 'Edit' button doesn't change status

匆匆过客 提交于 2019-12-09 05:22:58

问题


I have a UIViewController class, with a tableView. In viewDidLoad:

UIBarButtonItem *editIcon = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemEdit
                    target:self
                    action:@selector(toggleEditMode)] autorelease];

In te method 'toggleEditMode':

-(void)toggleEditMode{
if(self.theTable.editing) {
    [theTable setEditing:NO animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else if ([callsArray count]!=0){
    [theTable setEditing:YES animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}

}

The problem is that the Edit button does not change do 'DONE'. What's missing? I have all the methods declared:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Thanks,

RL


回答1:


Why not use -editButtonItem of UIViewController directly ? And override -setEditing:animated: method.

// Assign the system's edit button, 
// it will change style when different edit status.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

// The editButtonItem will invoke this method.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
         // Execute tasks for editing status
    } else {
         // Execute tasks for non-editing status.
    }
}



回答2:


It's because you're only setting the style of the button (not its text) when you switch modes. You need to do this (or equivalent):

-(void)toggleEditMode{
    self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";



回答3:


You sure callsArray is not empty ?

Place a breakpoint inside toggleEditMode and see what happens there.

Edit

Ok, after understanding your question, have a look at this thread



来源:https://stackoverflow.com/questions/5365074/uitableview-in-edit-mode-edit-button-doesnt-change-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!