2015-10-09 172 views
3

我正在尋找一個回調函數UITableView,當該行專注於當我用蘋果遙控器導航並按向上或向下按鈕來選擇行(而不是當我按下輸入時)。tvOS UITableView細胞集中

在iOS上我們有didSelectRowAtIndexPath這是當用戶選擇行時調用,但我找不到在用戶導航UITableView時調用的函數。

回答

14

您必須實現此委託方法:

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

,那麼你可以做任何你在該函數想用它

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