自定义默认导航编辑按钮(如何维护其状态?)

时间:2013-05-24 07:03:06

标签: ios uitableview uinavigationbar uinavigationitem

我有导航栏,我在其上设置了默认编辑按钮,可以让我删除表格项目。但是,当我的表为空时,它仍然将按钮的状态保持为“完成”并且不会返回“编辑” 我的代码在这里, 在viewDidLoad

 self.navigationItem.leftBarButtonItem = self.editButtonItem;
 //My Editing method 
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
 { 
    if([userInfoArr count]!=0)
    {
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:YES]; 
    }
 }

4 个答案:

答案 0 :(得分:2)

您可以使用tableView:commitEditingStyle:forRowAtIndexPath:方法执行此操作,如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Do the deletion...
    }

    // If the array is empty, turn off edit mode
    if ([userInfoArr count] < 1) {
        [super setEditing:NO animated:NO];
        [tableView setEditing:NO animated:NO]; 
    }
}

答案 1 :(得分:1)

然后删除最后一个表项,只需调用

[self.tableView setEditing:NO animated:YES];

在非编辑模式下设置表格视图。

也许你需要拨打[super setEditing:NO animated:YES];来设置处于“编辑”状态的按钮

答案 2 :(得分:1)

真棒它有效..!我不得不把它放在我的numberOfRowsInSection中,- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(userInfoArr.count==0) { [self.tableView setEditing:NO animated:YES]; [super setEditing:NO animated:YES]; } return userInfoArr.count; }

答案 3 :(得分:0)

我也遇到了这个问题。

我解决了这个问题
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

检查数据源是否为空。将编辑设置为NO

 [self.tableView setEditing:NO animated:YES];
 [self setEditing:NO];