2012-01-25 146 views
0

我有一個tableview添加到viewController(我想這樣)。 tableview的每個單元格都有一個段控制器,當用戶單擊它時,單元格將被刪除/刪除。從表中刪除單元格查看

我最終得到一個異常,我不知道如何解決它。

2012-01-23 15:35:26.729 TestProject [21003:707] *斷言故障 在 - [UITableView的_endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1448.89/UITableView.m: 995

2012-01-23 15:35:26.761 TestProject [21003:707] *終止應用程序 由於未捕獲的異常 'NSInternalInconsistencyException',原因: 「無效更新:在第0行的無效數。之後的現有節中包含的 行的數量更新(2)必須爲 ,其等於在 更新(2)之前該節中包含的行數,加上或減去從 該節(插入0,刪除1)插入或刪除的行數。

我的代碼到目前爲止;

在按鈕事件;

UITableViewCell *cell=(UITableViewCell *)segmentController.superview.superview; 

     NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell]; 

     Person *person= [[self allThdata] objectAtIndex:indexPath.row]; 

       [self.allThdata removeObjectIdenticalTo:person]; 

     [self.myTableView beginUpdates]; 

     [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 

     [self.myTableView endUpdates]; 

我自己也嘗試removeObjectAtIndex: indexPath.rowremoveObject: person。沒有任何工作

回答

1

致電removeObjectAtIndex:應在beginUpdates區塊內。

這應該做的伎倆:

UITableViewCell *cell=(UITableViewCell *)segmentController.superview.superview; 
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell]; 
[self.myTableView beginUpdates]; 
[[self allThdata] removeObjectAtIndex:indexPath.row]; 
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 
[self.myTableView endUpdates]; 
+0

我想,我想這一點,但'[自allThdata] removeObjectAtIndex:indexPath.row]'線高於'[self.myTableView beginUpdates]'。 – Illep

+0

它應該在'[self.myTableView beginUpdates];'下面。 –