在其中一个视图中有一个UITableView,它经常被更新。 使用“reloadRowsAtIndexPaths”
以经典方式跟踪更改-(void)refreshCells:(NSArray *)changedCells
{
NSLog(@"refreshCells %i",[changedCells count]);
[TableView beginUpdates];
[TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom];
[TableView endUpdates];
}
问题:如何保留用户上次选择的单元格。每次更新refreshCells后,单元格位置可能会发生变化吗?
答案 0 :(得分:9)
您可以使用
保存当前选择NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];
重新加载之前,再次使用
选择它if (selectedRow) {
[self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}
重装后。除非您致电insertRowsAtIndexPaths:
或deleteRowsAtIndexPaths:
,否则单元格位置不会更改。