Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tvOS UITableView cell focused

I'm searching for a callback function of UITableView when the row is focused for when I navigate with apple remote and press up or down to select the row (not when I press enter).

On iOS we have didSelectRowAtIndexPath thats called when user selects the row but I can't find the function that is called when user navigates in the UITableView.

like image 592
Danny Avatar asked Oct 09 '15 22:10

Danny


2 Answers

You must implement this delegate method:

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
     //this gives you the indexpath of the focused cell
     NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
}

then you can do whatever you want with it in that function

like image 154
Yannis P. Avatar answered Oct 13 '22 18:10

Yannis P.


Testing this in mid-2019, nextFocusedIndexPath does not seem to be available from the context object anymore. But it wasn't too hard to work around:

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
  let cell = context.nextFocusedView as! UITableViewCell
  let indexPath = table.indexPath(for: cell)
}
like image 29
Casey Perkins Avatar answered Oct 13 '22 16:10

Casey Perkins