2009-05-27 51 views
16

這是我想出來的:如何手動刪除UITableView中的一行?

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; // my table view has 2 sections 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 

每次我建立和運行,它拋出以下異常:

無效更新:在第0的數行數無效更新後現有節中包含的行必須等於更新前該節中包含的行數,加上或減去從該節添加或刪除的行數。

這有點令人困惑。該部分設置爲1,但例外說它是0.

+4

愛你線NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0切入口:1] ;.爲你+1;) – oberbaum 2010-01-28 16:27:55

回答

29

我想通了。

除了上述代碼,我還需要修改的數據源

[items removeObjectAtIndex:0]; 
+2

這是正確的。 – 2009-05-27 20:41:18

+8

對於任何曾經收到錯誤的人,像我一樣,在OP中說過;請確保在更改UITableView的內容之前更改您的數據模型。 – Sean 2011-10-03 12:22:39

5
- (IBAction)deleteCustomCellWithUIButton:(id)sender 
{ 
    NSLog(@"Message From Custom Cell Received"); 
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]]; 
    NSUInteger row = [indexPath row]; 
    [self.myDataArray removeObjectAtIndex:row]; 
    [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
}