2013-07-29 22 views
4

我知道這已在50次之前問過,但我查看了所有其他答案,但未找到任何可解決我的問題的內容。無效更新:第0部分中的行數無效UITableView

反正這是我的代碼:

NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init]; 
     for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) { 
      NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0]; 
      if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) { 
       [cellIndicesToBeDeleted addObject:p]; 
       [self.cellArray removeObjectAtIndex:i]; 
      } 
     } 
     [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft]; 
     [cellIndicesToBeDeleted release]; 

當我執行這個代碼,我得到這個崩潰日誌:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).' 

有誰看到我做錯了嗎?

稍新代碼:

NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init]; 
     NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init]; 
     for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) { 
      NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0]; 
      if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) { 
       [cellIndicesToBeDeleted addObject:p]; 
       [mutableIndexSet addIndex:p.row]; 
      } 
     } 
     [self.cellArray removeObjectsAtIndexes:mutableIndexSet]; 
     [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft]; 
     [cellIndicesToBeDeleted release]; 
     [mutableIndexSet release]; 
+0

這聞起來就像它只是有不好的邏輯。例如,你爲什麼要自己調用'UITableViewDelegate'方法?回到繪圖板,找出你想要做什麼。也許在這個問題中勾勒出你的目標是很有用的。就像,你展示了什麼樣的數據?爲什麼刪除一些項目時,他們有一個複選標記? –

+0

它來自這個問題的第一個答案:http://stackoverflow.com/questions/6227168/edit-delete-multiple-rows-in-uitableview-simultaneously –

+0

這與你的問題沒有關係,但你會打這個錯誤一旦你的同步錯誤結束:如果你有兩個標記的相鄰行,第二行將被跳過。這是因爲你前後迭代:在一個索引處刪除一個對象將它的鄰居移到它的位置,但是循環增加索引「i」,所以你跳過下一個鄰居。向後迭代數組以解決此問題。 – dasblinkenlight

回答

4

我想通了,它同numberOfRowsInSection做。我沒有更新我的nsuserdefaults與更新的數據源,所以我現在做了,它的工作完美。這是我的更新代碼的方式:

//Code to delete rows 
     NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init]; 
     NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init]; 
     for (int i = [thetableView numberOfRowsInSection:0] - 1; i >= 0; i--) { 
      NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0]; 
      if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) { 
       [cellIndicesToBeDeleted addObject:p]; 
       [mutableIndexSet addIndex:p.row]; 
      } 
     } 
     [self.cellArray removeObjectsAtIndexes:mutableIndexSet]; 
     [[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"]; 
     [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft]; 
     [cellIndicesToBeDeleted release]; 
     [mutableIndexSet release]; 
3

您需要添加之前下列和刪除行後:

[theTableView beginUpdates]; 
// your code goes here 
[theTableView endUpdates]; 
+0

沒有解決它。 :( –

+0

請確保您添加了代碼以從數據源中刪除indexPath以及begin/endUpdates內部,而不僅僅是tableView deleteRows。 –

相關問題