2016-08-03 97 views
0

強調textreason:滑動刪除的UITableView細胞

Invalid update: invalid number of rows in section 0. 
The number of rows contained in an existing section after 
the update (5) must be equal to the number of rows contained in 
that section before the update (5), plus or minus the number of 
rows inserted or deleted from that section (0 inserted, 1 deleted) 
and plus or minus the number of rows moved into or out of that 
section (0 moved in, 0 moved out). 

***第一擲調用堆棧

我,當我試圖刪除

回答

0

你的問題還不清楚,按照這個錯誤這些步驟

  1. 在委託方法commitEditingStyle中,從數組中刪除對象。
  2. 然後在更新後的數組上應用deleterow動畫。

我認爲üR值不要從您的收藏即陣列

1

刪除對象時,此錯誤意味着,當你更新刪除後的UITableView,您正在使用的數據源尚未更新的陣列。

換句話說:

你有NSArray的dataArray你是在你的UITableViewDelegate使用cellForRowAtIndexPath。 您動畫單元刪除的行,但你永遠不會調用

[dataArray removeObjectAtIndex:index];

所以當表再次呈現,在預計的UITableView那裏是5個項目,但你的陣列仍然有6

0

試試這個代碼。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableDataArray removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tableView reloadData]; 
} 
} 


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 

} 

對於更多的參考,您可以訪問here