好吧,我卡住了。這是我以前的職位的延伸。這是我想要做的。使用insertRowsAtIndexPaths時表格視圖單元格索引
我有一個導航欄上的編輯按鈕,當按下時,在我的一節表格視圖的開始添加一個單元格。這個單元格的目的是爲了允許用戶向表中添加新的數據;因此它的編輯風格是Insert。表格中的其餘單元格使用編輯樣式「刪除」進行配置。
這裏是我的setediting方法:
- (IBAction) setEditing:(BOOL)isEditing animated:(BOOL)isAnimated
{
[super setEditing:isEditing animated:isAnimated];
// We have to pass this to tableView to put it into editing mode.
[self.tableView setEditing:isEditing animated:isAnimated];
// When editing is begun, we are adding and "add..." cell at row 0.
// When editing is complete, we need to remove the "add..." cell at row 0.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray* path = [NSArray arrayWithObject:indexPath];
// fill paths of insertion rows here
[self.tableView beginUpdates];
if(isEditing)
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
else
[self.tableView deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
// We nee to reload the table so that the existing table items will be properly
// indexed with the addition/removal of the the "add..." cell
[self.tableView reloadData];
}
我佔我的代碼,這個額外的電池,但我現在有兩個索引路徑= [0,0] - 新電池和舊原第一表中的單元格。如果我添加一個調用來重新加載setEditing中的表格視圖單元格,單元格會重新編制索引,但現在我的表格視圖不再動畫了。
我想要我的蛋糕,也吃了它。是否有另一種方法來完成我想要做的事情並保持動畫?
--John
我已經看過示例代碼,除了新的單元格添加在表的末尾。不是我想要的 - 我希望在桌子的開始。 – johnnyspo
目前尚不清楚問題是什麼以及你想要完成什麼。 – XJones
以地址簿爲例。編輯聯繫人時,在「地址」部分的末尾添加一個單元格「添加新地址」,並在其中添加「+」編輯樣式。我想要做類似的事情,除了我想在該部分開頭的新單元格。我可以通過在indexPath [0,0]處插入單元格來輕鬆完成此操作。但是這給了我兩個[0,0]的單元格(新的單元格和[0,0]的原始單元格)。我可以解決這個問題,重新加載表格(重新索引單元格),除非當我按下編輯按鈕時表格不再動畫。 – johnnyspo