tvOS UITableView 以单元格为中心

tvOS UITableView cell focused

我正在搜索 UITableView 的回调函数,当我使用 apple remote 导航并向上或向下按到 select 行时(不是当我按 enter 时) ).

在 iOS 上,我们有 didSelectRowAtIndexPath 当用户 select 行时调用的函数,但我找不到当用户在 [=10= 中导航时调用的函数].

您必须实现此委托方法:

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

然后你就可以在那个函数中用它做任何你想做的事了

以防万一你想用最后一个选择做某事,你可以这样做

NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
NSIndexPath *prevIndexPath = [context previouslyFocusedIndexPath];

UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
UILabel *nextTitleLabel = (UILabel *)[nextCell viewWithTag:100];
nextTitleLabel.textColor = [UIColor blackColor];

UITableViewCell *prevCell = [self.tableView cellForRowAtIndexPath:prevIndexPath];
UILabel *prevTitleLabel = (UILabel *)[prevCell viewWithTag:100];
prevTitleLabel.textColor = [UIColor whiteColor];

在 2019 年年中对此进行测试,上下文对象似乎不再提供 nextFocusedIndexPath。但解决起来并不难:

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
  let cell = context.nextFocusedView as! UITableViewCell
  let indexPath = table.indexPath(for: cell)
}