2010-07-22 39 views
0

我試圖通過滑動手指(如最近調用)來刪除tableView上的單元格。我知道我需要實現:如何刪除手指在iPhone上滑動的單元格

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{ 
    NSArray *index = [[NSArray alloc]initWithObjects:indexPath]; 
    [self.tableView deleteRowsAtIndexPaths:index withRowAnimation: UITableViewRowAnimationNone]; 
} 

但是,當我滑動手指,我得到這個功能和第二排,當我試圖刪除單元格的得到錯誤:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).

有人可以幫忙嗎?

回答

2

當你調用

[self.tableView deleteRowsAtIndexPaths:index withRowAnimation: UITableViewRowAnimationNone]; 

表視圖會嘗試重新加載,這反過來又調用委託方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

既然你已經刪除一行,進行必要的動畫才能正常工作,表格視圖需要按照您刪除的行數相同的行數進行更改deleteRowsAtIndexPaths:withRowAnimation:。您收到的錯誤消息試圖解釋依賴關係。這是說你在表視圖有行,你刪除 ...但是當表視圖問你在刪除後多少行了,你又說了一遍,而不是4-1 = 3它預計...然後它開始關閉本身大概是因爲it did not want to live in a world where the laws of natural numbers could not explain reality

例如,如果您使用的是的NSArray *來填充您的tableView,你應該在同一時間進行刪除該數組中的相應的對象,你在呼喚deleteRowsAtIndexPaths:withRowAnimation:

相關問題