Alex Rivera | Logout

How do I maintain the UITableView selected row when returning from a UINavigationController child screen?

Asked 2011-07-22T19:42:02.250
10

I have a UITableView of customers where a row/customer is selected. The user can then push another view on the UINavigationController stack to add a new customer. When I pop the child screen and return to the UITableView the previously selected row is no longer selected.

I can re-select the row in viewDidAppear() but it looks bad as you can see the deselect and the select. Is there a way to maintain the selected row when returning from the child screen?

Edit
Report

3 Answers

0

This is possible :

Only in once case: If you have the static content in TAbleView,

just reload the TableView in ViewDidLoad only, not in View will Appear.


whereas make sure, you don't allow table to reload anywhere in same class in any method. Then you can achieve what you want.

The cell get deselect because of reload the TAbleView.

But for Dynamic content, you need to use the delegate method:

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

In this you need to pass the indexPath which you want to show selected with the scrollPostion same as tableviewposition.

answered 2011-07-23T12:09:20.833
-2

You cannot have a tableViewCell selected all the time. It gets back to normal once it serves its purpose. I think you meant highlighted or appear to be selected, and I am answering the question based on that.

Get the row that was selected at the cellForRowAtIndexPath. When you pop your navigation controller and come back to the tableView, go the the cellForRowAtIndexPath and have that cell highlighted (You can use any UIColor as you wish) - there is no specific way to highlight stuff - use your imagination - Default highlight style is Blue, while the text is White.

How is this done ?

  1. Find selected row in cellForRowAtIndexPath
  2. Store is in an external variable / or NSUserDefaults and you can also use NSUserDefaultsto communicate between screen to find out if item was popped from that screen.
  3. [tableView reloadData] in viewWillAppear
  4. Check for NSUserDefaults value in cellForRowAtIndexPath and modify that cell with cell.backgroundColor or something.
answered 2011-07-22T19:54:16.413
-3

Without some code, I can just give an advice : stop using [tableView reloadData], use [tableView beginUpdates], [tableView endUpdates] and – insertRowsAtIndexPaths:withRowAnimation: and – reloadRowsAtIndexPaths:withRowAnimation:. Things should work as expected.

answered 2011-07-22T20:26:18.030

Your Answer