【发布时间】:2015-10-09 22:30:51
【问题描述】:
我正在搜索UITableView 的回调函数,当我使用苹果遥控器导航并按向上或向下键选择行时(而不是按回车键)。
在 iOS 上,我们在用户选择行时调用了 didSelectRowAtIndexPath,但我找不到用户在 UITableView 中导航时调用的函数。
【问题讨论】:
标签: uitableview tvos
我正在搜索UITableView 的回调函数,当我使用苹果遥控器导航并按向上或向下键选择行时(而不是按回车键)。
在 iOS 上,我们在用户选择行时调用了 didSelectRowAtIndexPath,但我找不到用户在 UITableView 中导航时调用的函数。
【问题讨论】:
标签: uitableview tvos
你必须实现这个委托方法:
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
//this gives you the indexpath of the focused cell
NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
}
然后你可以在那个函数中做任何你想做的事情
【讨论】:
在 2019 年年中对此进行测试后,上下文对象似乎不再提供 nextFocusedIndexPath。但是解决起来并不难:
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
let cell = context.nextFocusedView as! UITableViewCell
let indexPath = table.indexPath(for: cell)
}
【讨论】:
以防万一你想对最后一个选择做某事,你可以这样做
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];
【讨论】: