我有一個ViewController創建一個新的對象,然後返回到FirstViewController.m我有一個unwind方法應該更新tableView和一個新的對象的行。使用reloadData方法將無法工作,因爲它會重新加載影響我在這些單元中的方法的所有單元。我想只是添加一個新的單元格而不重新加載整個tableView。我的開卷方法是這樣的:在不使用reloadData的情況下添加一行到UITableView
-(IBAction)unwind:(UIStoryboardSegue *)segue {
ABCAddItemViewController *source = [segue sourceViewController];
ABCItem *item = source.object;
if (item != nil) {
[self.objectArray addObject:item];
[self.tableView reloadData];
}
}
我試着加上幾行,而不是[self.tableView reloadData]:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.objectArray indexOfObject:item] inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
然而,應用程序崩潰,我得到這個消息:
更新無效:第0節中的行數無效。更新(1)後現有節中包含的 行的數量必須爲 ,等於所包含的行數n在 更新(1)之前的部分,加上或減去從 插入或刪除的行的行數(1插入,0刪除)以及加或減行數 移入或移出該部分的行數(0搬進來,0搬出去)。
tableView中的行數由the numberOfRowsInSection:
方法中的[self.objectArray count]
決定。
我該如何做這項工作?或者,還有更好的方法?
你需要更新你的數據源('self.objectArray ')在調用'insertRowsAtIndexPath'之前使用附加數據。 – rmaddy
我試圖只更換'[self.tableView reloadData]'行,但是產生了我提到的錯誤。我在此之前將對象添加到數組中。 – Benck
對'reloadData'的調用不會給出您發佈的錯誤。這隻會來自對'insertRowsAtIndexPaths:'的調用。順便說一句 - 您需要將行插入到將其添加到陣列的同一位置。 – rmaddy