开发者

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

开发者 https://www.devze.com 2023-03-22 13:38 出处:网络
I have a UITableView of customers where a row/customer is selected. The user can then push another view on the UINa开发者_StackOverflow社区vigationController stack to add a new customer. When I pop th

I have a UITableView of customers where a row/customer is selected. The user can then push another view on the UINa开发者_StackOverflow社区vigationController 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?


I assume you are using a table view controller, as otherwise it is your responsibility to write this behaviour anyway. In a table view controller, though, it's easy. Just add in viewDidLoad:

self.clearsSelectionOnViewWillAppear = NO;

That will keep the row selected, unless you manually deselect the row or the user selects another row on the table.


If you select it in viewWillAppear:, it should get selected without the user seeing it. Would that solve your problem?


You need to call:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

This is found in the didSelectRowAtIndexPath delegate method for a UITableView.


By default, a selected row gets deselected (with animation) when you return to that VC.
So, if you push a DetailViewController, the cell stays selected until you return to the RootViewController and then gets deselected automatically.

I used to have such a problem too. Make sure to always call the superclass's implementation of methods such as viewWillAppear, viewWillDisappear etc.
Also, do not call deselectRowAtIndexPath: manually.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消