我从表视图中删除一行时遇到问题。这是按下行上的按钮时调用的代码。我使用 tag 参数告诉我正在处理哪一行,这就是为什么我在最后调用 reloadTableView 以重新应用所有标签。如果我更新可见行,我就会崩溃。所以我一定在这里做错了什么?
- (void) deleteLineItem:(id)sender
{
UIButton *deleteBtn = sender; // Need to get tag (which was set to table row)
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(deleteBtn.tag/3) inSection:0];
NSLog(@"Removed entry at %d, with tag %d", indexPath.row, deleteBtn.tag);
[self.productItems removeObjectAtIndex:indexPath.row];
[self.tableview beginUpdates];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableview endUpdates];
// Hack to keep table animation ok as we have to do it after the row delete above
[self performSelector:@selector(reloadTableview) withObject:nil afterDelay:0.35];
}
在这种情况下,我在表中有两行。数据源如下:
Printing description of self->productItems:
(__NSArrayM *) productItems = 0x084fa3b0 @"2 objects"
索引路径如下:
Printing description of indexPath:
<NSIndexPath 0x1069bff0> 2 indexes [0, 1]
当调用 endUpdates 方法时,我在控制台中遇到以下崩溃
2012-10-17 17:04:33.424 myAppname[5782:15203] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x1bf0022 0x1918cd6 0x1bdcd88 0xa5553 0xa576b 0x925420 0x963284 0x9633d0 0x925219 0x8fd4f2 0x8fa4c3 0x9050b7 0x9050e5 0xa6ae1 0x1bf1e99 0x87814e 0x8780e6 0x91eade 0x91efa7 0x91e266 0xb39a1a 0x1bc499e 0x1b5b640 0x1b274c6 0x1b26d84 0x1b26c9b 0x25007d8 0x250088a 0x875626 0x1f2ba 0x2ba5)
terminate called throwing an exception(lldb)
我不知道为什么会导致问题。