我有一個問題,從表視圖中刪除一行。這是在按下行上的按鈕時調用的代碼。我使用了tag參數來告訴我正在處理哪一行,這就是爲什麼我在最後調用reloadTableView來重新應用所有標記的原因。如果我更新可見的行,我有崩潰。所以我一定在這裏做錯了什麼?從UITableView刪除行導致崩潰
- (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"
的indexPath如下:
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)
我有不知道爲什麼會造成問題。
其實我真的不喜歡你的哈克...:) – Peres
我也沒有,但我不能讓動畫正確地工作沒有它 – d0n13
你也可以發佈代碼的一部分,這個'deleteLineItem:'被調用? –