How to remember rows selected and maintain them after reload by default in UITableView?
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
will give you the NSIndexPath
for the current selected row. You can then do
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
to select that row (and show it in the middle of the screen) later.
I had the same question, and this is how I did it:
In your tableViewController
, add this in this function
(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == 'the row you want to select') { [tableView selectRowAtIndexPath:indexPath animated:TRUE scrollPosition:UITableViewScrollPositionNone ]; [[tableView delegate] tableView:tableView didSelectRowAtIndexPath:indexPath ]; }}
This will do it:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Where row is the cell number you want to select (starts 0...), and section is the section where the cell appears.