11

我有一个 UIViewController 类,带有一个 tableView。在 viewDidLoad 中:

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

在方法“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];
}

}

问题是“编辑”按钮不会更改为“完成”。少了什么东西?我声明了所有方法:

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

谢谢,

强化学习

4

3 回答 3

22

为什么不-editButtonItem直接使用 UIViewController 呢?并覆盖-setEditing:animated:方法。

// 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.
    }
}
于 2011-03-20T00:23:42.893 回答
2

这是因为您在切换模式时只设置按钮的样式(而不是其文本)。您需要这样做(或等效):

-(void)toggleEditMode{
    self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";
于 2011-03-19T22:14:00.623 回答
0

你确定callsArray不是空的?

在里面放置一个断点toggleEditMode,看看那里会发生什么。

编辑

好的,理解你的问题后,看看这个线程

于 2011-03-19T21:58:44.183 回答