2009-09-21 53 views
4

你知道在表格進入編輯模式後,如何讓一些單元格出現在表格視圖中?就像編輯聯繫人時的「聯繫人」iPhone應用程序一樣。如何在編輯模式下在UITableView中添加額外的單元格?

也許我錯了,但編輯聯繫人時,它看起來像使用了分組的​​UITableView。

我嘗試這樣做:

[self.tableView setEditing:YES animated:YES]; 
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom]; 

我的表才1時,不對其進行編輯部分,我想添加一個額外的部分編輯時(保持簡單),但在調用上面的「insertSections」崩潰(我所有的表代表考慮到1或2個部分,這具體取決於self.editing,我試圖顯示在正常模式下兩個部分,並能正常工作)

對於「numberOfSectionsInTableView」我有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (!self.editing) return 1; 
    return 2; 
} 

回答

4

你在哪裏輸入了tableView的編輯狀態?你應該這樣做的 - [UINavigationController的setEditing:動畫:]:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; 

    if(editing){ 
     [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom]; 
    } else { 
     // delete section 
    } 
} 

因此,而不是對的tableView調用setEditing的,把它的navigationController(可能self),則控制器將處理編輯模式。

所以控制器有一個編輯模式,和tableView有。一旦您以編程方式設置,或者當用戶滑動刪除一行時,tableView將處於編輯模式。在後一種情況下,控制器而不是處於編輯模式,但表查看

+0

非常好!事實上,這個問題與編輯模式中的內容混淆了。非常感謝! – 2009-09-21 16:59:38

+0

我必須繼承UINavigationController嗎?或者我在哪裏放置了您的代碼?謝謝 – JonEasy 2012-07-14 06:07:57

+0

此處顯示的代碼用於「UITableViewController」的子類,該子類可能是「UINavigationController」的子控制器。 'UINavigationController'處理顯示導航欄和當前視圖(控制器),你不需要它的子類。然而'UITableViewController'處理tableview的所有信息,所以你確實需要該類的一個子類。 – Joost 2012-07-14 12:56:24

相關問題