Search code examples
ios5uitableviewnsindexpath

Change NSIndexPath when navigating back to UITableView


SCENARIO (iOS 5)
I currently have a UITableView.
It has 30 table rows
The user decides to click on row 5
A ViewController is pushed.
Within this ViewController the user has navigated forward 10 items.
When they navigate back to the UITableView they are at row 5 highlighted

What I would like
I want the UITableView to show table row 15 highlighted and in view, because they have moved 10 rows ahead within the ViewController.

What I found
I thought I would be able to modify the NSIndexPath row property to 15 when I navigate back. But documentaiton states it's read-only.

row  
An index number identifying a row in a section of a table view. (read-only)  

@property(readonly) NSInteger row  

Discussion
The section the row is in is identified by the value of section.

Any feedback on how I can get around this?


Solution

  • If you just want to change the selection, use this UITableView method:

    - (void)
    selectRowAtIndexPath:(NSIndexPath*)indexPath
    animated:(BOOL)animated
    scrollPosition:(UITableViewScrollPosition)scrollPosition;
    

    You don't need to modify a particular index path to call that API, just create a new one; for example:

    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];