Keep selected cell on heavily reloadRowsAtIndexPaths UITableView

做~自己de王妃 提交于 2019-12-10 13:53:18

问题


In one of the views there is a UITableView which is getting updated rather often. Tracking the changes are done in a classic way using "reloadRowsAtIndexPaths"

-(void)refreshCells:(NSArray *)changedCells
{
    NSLog(@"refreshCells %i",[changedCells count]);
    [TableView beginUpdates];
    [TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom];
    [TableView endUpdates];
}

Question: How can I preserve the user's last selected Cell. the cell position may change after each update refreshCells?


回答1:


You can save the current selection with

NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];

before the reload and select it again with

if (selectedRow) {
    [self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}

after the reload. The cell position does not change unless you call insertRowsAtIndexPaths: or deleteRowsAtIndexPaths:.



来源:https://stackoverflow.com/questions/13045897/keep-selected-cell-on-heavily-reloadrowsatindexpaths-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!