tvOS的UITableView单元格聚焦

7

我正在寻找UITableView的回调函数,当行聚焦时,我使用苹果遥控器导航并按上下键选择行时调用(不是当我按回车键时)。

在iOS中,我们有didSelectRowAtIndexPath,当用户选择行时调用,但我找不到当用户在UITableView中导航时调用的函数。

3个回答

21

你必须实现这个委托方法:

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

那么在该函数中,您可以对其进行任何操作。


1

在2019年中期进行测试时,似乎无法从上下文对象中获取nextFocusedIndexPath。但是绕过这个问题并不太困难:

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

0

如果您想对最后一个选择的内容进行操作,可以按照以下方式进行

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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接