Reload a table view's data without clearing its selection state
You can store the index path of the selected row with:
rowToSelect = [yourTableView indexPathForSelectedRow];
Before reload the data. And after reload use:
[yourTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
JeroVallis solution works for single selection table views. Based on his idea this is how I made it work with multiple selection:
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
[tableView reloadData];
for (int i = 0; i < [selectedIndexPaths count]; i++) {
[tableView selectRowAtIndexPath:selectedIndexPaths[i] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
The most efficient way is to keep the selected state in the data model
- Add a boolean property
isSelected
in the struct or class which represents the data source. - In
cellForRowAt
set the selected state of the cell according to the property. - In
didSelectRow
toggleisSelected
in the data source item and reload only the particular row at the given index path.