State Restoration for TableView selected row not working

  • First
    BNRItemsViewController does not display the last selected row by the user when e.g you go back from BNRItemsDetailViewController, in BNRItemsViewController.viewDidLoad I am setting self.clearsSelectionOnViewWillAppear = NO, table data is getting reloaded in BNRItemsViewController.viewWIllAppear and I am hoping that the TableViewController will automatically manage the selection of rows, but when the view appears nothing gets selected.

  • Second
    The above does not work so I assume that the code in the book which should restore state for the selected row does not work after you run the application and trigger state restoration.

I have tried manually selecting row using the tableView selectRowatIndexPath method, but even that has not worked with State Restoration, highlighting the last selected row by the user in the tableView is very important, so can someone from Big Nerd Ranch throw some light on this.


the solution, was suspicious of tableview.reloadData in ViewWillAppear and it does clear the tableview.indexPathForSelectedRow.
Below is the ViewWillAppear code, Run this and capture tableview.indexPathForSelectedRow before reloadData as shown below,
have to manually select row if you reloadData.

-(void) viewWillAppear:(BOOL)animated
[super viewWillAppear];

// capture the indexPath
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];

[self.tableView reloadData];

// select the row
if (self.indexPath)
[self.tableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];


State Restoration for tableView selected row should also work now.