如何手动删除UITableView中的行?

Anh*_* Do 17 iphone cocoa-touch objective-c uitableview

这是我想出的:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; // my table view has 2 sections
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
Run Code Online (Sandbox Code Playgroud)

每次我构建和运行时,它都会抛出以下异常:

无效更新:第0节中的行数无效.更新后现有部分中包含的行数必须等于更新前该部分中包含的行数,加上或减去添加或删除的行数那一节.

这有点令人困惑.该部分设置为1,但异常表示它为0.

Anh*_* Do 29

我想到了.

除了前面提到的代码,我还需要对数据源进行更改

[items removeObjectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

  • 对于任何收到错误的人,如我所说,在OP中说明; 在更改UITableView的内容之前,请务必更改数据模型. (8认同)

Chr*_*zzz 6

- (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];
}
Run Code Online (Sandbox Code Playgroud)