【问题标题】:How to delete a row in UITableView manually?如何手动删除 UITableView 中的一行?
【发布时间】:2009-05-27 10:48:32
【问题描述】:

这是我想出的:

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

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

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

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

【问题讨论】:

  • 喜欢你的行 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];。为你 +1 ;)

标签: iphone objective-c cocoa-touch uitableview


【解决方案1】:

我想通了。

除了上述代码,我还需要对数据源进行修改

[items removeObjectAtIndex:0];

【讨论】:

  • 对于像我一样收到错误的任何人,在 OP 中说明;请务必更改您的数据模型before 更改 UITableView 的内容。
【解决方案2】:
- (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];
}

【讨论】:

  • 我建议您在开始更改之前添加[self.myTableView beginUpdates];。完成后[self.myTableView endUpdates];
猜你喜欢
  • 2016-02-04
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
相关资源
最近更新 更多