Store selected cell on heavily reloaded RowsAtIndexPaths UITableView

One view has a UITableView, which is often updated. Change tracking is done in the classic way using "reloadRowsAtIndexPaths"

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

Question. How to save the last cell saved by the user. Can the cell position change after every update of the Cells update?

+4
source share
1 answer

You can save the current selection with

 NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow]; 

before rebooting and select it again with

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

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

+9
source

Source: https://habr.com/ru/post/1441764/


All Articles