2011-03-19 84 views
11

我有一个UIViewController类,有一个tableView。在viewDidLoad中:UITableView在编辑模式 - '编辑'按钮不会改变状态

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

在TE法 '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 

感谢,

RL

回答

22

为什么不直接使用UIViewController的-editButtonItem?并覆盖-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. 
    } 
} 
+0

完美。 只是(编辑)if(theTable.editing),因为de tableView是一个IBOutlet只是改变。该类不是从UITableViewController继承的。 谢谢, RL – 2011-03-20 14:17:04

+0

不客气。 :) – AechoLiu 2011-03-20 21:58:04

+1

尽管这是一个老问题,但它帮助我意识到我忘了[super setEditing:editing animated:animated];这是我矿难的原因。 ;)感谢您发布此信息。 – DataJock 2013-06-21 17:12:37

0

你肯定callsArray是不是空的?

toggleEditMode内放置一个断点,看看会发生什么。

编辑

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

+0

是的,我确定。表格在编辑模式下进入,当我删除一行时它就起作用。这不是问题。 问题是,'编辑'按钮并没有改变它的名字'DONE'在类是一个UITableViewController类时做了。相反,我的类是一个带有表格的UIViewController。 – 2011-03-19 22:02:05

2

这是因为你只设置按钮的风格(而不是它的文字)时你切换模式。您需要这样做(或等效):

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

为什么不像当一个类从UITableViewController继承时那样工作? 我无法将按钮分配到桌面并按照这种方式工作吗? – 2011-03-19 22:22:33