Search code examples
iosobjective-cuitableviewfocustvos

UITableView and new methods for tvOS


I am creating a test tvOS app. I have an image (outside the tableview) and when "focused" NOT selected, I would like the image to change... I know that apple has added a couple methods for focus. Could someone tell me which methods I would use to change this image for when it is in "focus"? and how would I use it? Would I use this method:

   - (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath

or would I use:

  - (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator

Please let me know! Thank you!


Solution

  • If you want to change image when UITableViewCell item get focus, then you should use second method.

     - (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
    {
       //You can get focused indexPath like below
       NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
       // Now get image from data source, I will assume you have an images array
       UIImage *image = [_imagesArray objectAtIndex:nextIndexPath.row];
       // Set image to image view, assuming you have a property imageView
       _imageView.image = image;
    }