UITableView reloadRows resets focus

I have a UITableViewController shared between my iOS and tvOS apps. In order to refresh some of the cells, I call reloadRows. It works fine as far as updating the cell's data but, on tvOS, it moves the focus back to the top of the table. I've noticed that preferredFocusEnvironments does not get called (unlike what happens when I do reloadData).

This is pretty much the same issue mentioned in the following post (years ago): https://developer.apple.com/forums/thread/67219

Any suggestions?

Accepted Reply

Ok, I may have found a solution by using UITableViewDelegate's indexPathForPreferredFocusView instead of relying on preferredFocusEnvironments.

  • indexPathForPreferredFocusView is called from preferredFocusEnvironments. So that is odd. Are you sure you were overriding preferredFocusEnvironments on the table view and not somewhere higher up the chain? The focus update is likely scheduled on the table view itself, so nothing higher up the chain gets asked about a preference.

  • I was using the UITableViewController's preferredFocusEnvironments - I guess that would explain the issue.

Add a Comment

Replies

Maybe you could save the focus before calling and restore just after ?

  • I get no opportunity to 'restore it' after as preferredFocusEnvironments is not called.

Add a Comment

Ok, I may have found a solution by using UITableViewDelegate's indexPathForPreferredFocusView instead of relying on preferredFocusEnvironments.

  • indexPathForPreferredFocusView is called from preferredFocusEnvironments. So that is odd. Are you sure you were overriding preferredFocusEnvironments on the table view and not somewhere higher up the chain? The focus update is likely scheduled on the table view itself, so nothing higher up the chain gets asked about a preference.

  • I was using the UITableViewController's preferredFocusEnvironments - I guess that would explain the issue.

Add a Comment