如果您在使用主從應用程序創建一個新的簡單的項目,你可以看到,當你插入不滾動到頂部的內容偏移僅改變了一點(等於新的行高),即使你是在中間,頂部或底部插入頂部。 。它採用簡單的代碼一樣
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
現在,即使你不希望小滾動here是很好的解決方案
//just need check if you already on the top of the tableview that may be first few rows depend on your row height
if(self.tableView.contentOffset.y > ONE_OR_X_ROWS_HEIGHT_YOU_DECIDE
{
self.delayOffset = self.tableView.contentOffset;
self.delayOffset = CGPointMake(self.delayOffset.x, self.delayOffset.y+ insertedRowCount * ONE_ROW_HEIGHT);
//insert your row here
[self.tableView setContentOffset:self.delayOffset animated:NO];
}else
{
//insert your row here
}
謝謝快速答覆。在某些情況下,在插入時間(動態細胞高度)之前,我們不知道確切的細胞行高度。爲了確定,我們必須手動計算每個單元格的高度,並可能重複UITableView的工作。 – Dummy307
您是否嘗試過第一個解決方案,因爲它只滾動一行高度 – Imran
啊,我想要一個通用解決方案。因爲在我的情況下,我不得不一次批量更新(插入,刪除,更新)大量的單元格:)。 – Dummy307