2011-03-19 130 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