Runeaway3
Runeaway3

Reputation: 1449

How to get back button to return to same position in UITableView

I have a UITableView which has a segue set up to send the user to a new VC, when any of the cell is selected in the table. This segue works fine. The new VC has a "Back" button that sends the user back to the table, but this button always sends the user back to the top of table. Is there any way (perhaps programmatically) to get the back button to return the user to the table, but at the cell they previously selected (ie halfway down the table)?

Upvotes: 2

Views: 800

Answers (2)

UdayM
UdayM

Reputation: 1783

I faced the same situation. Then

  1. In didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,i am remembering cell selected to indexPathSelected

  2. I am reloading tableview from viewWillAppear

- (void)viewWillAppear:(BOOL)animated{
   [[self tableView] reloadData];
}

3. I am scrolling tableView to the position from ViewDidAppear

 [self.tableView scrollToRowAtIndexPath:indexPathSelected 
                  atScrollPosition:UITableViewScrollPositionTop animated:YES];

otherwise same behaviour you can get very easily with the help of contentOffset ,and you can reuse it when you came back.

Upvotes: 4

Janmenjaya
Janmenjaya

Reputation: 4163

Let me add this as an answer.

Code :

 [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

Before this line of code, you need to keep track of the index path that you clicked, so that you can pass the indexPath in the parameter to move your table to required position.

Upvotes: 1

Related Questions