tableview,更改焦点 tvos 的字体颜色

tableview, change font color of focus tvos

如何更改 "focused" UITableView 单元格的字体颜色?我目前有这个....

 - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
if (context.nextFocusedView) {
    CategoryCell *cell = [self.categoryTableView dequeueReusableCellWithIdentifier:@"CategoryCell"];
    [cell.categoryLbl setTextColor:[UIColor orangeColor]];
     }
 }

我知道如果你想改变聚焦的 UITableViewCell 的背景,它会是:

   - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    [context.nextFocusedView setBackgroundColor:[UIColor clearColor]];
[context.previouslyFocusedView setBackgroundColor:[UIColor orangeColor]];
 }

TVOSUITableViewDelegate中有新的方法tableView:didUpdateFocusInContext:withAnimationCoordinator:,当焦点改变时会被调用,你可以得到之前的IndexPath和nextFocusIndexPath,然后你可以使用tableView方法来获取单元格,你的代码应该是这样的

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    NSIndexPath *prevIndexPath = [context previouslyFocusedIndexPath];
    if (prevIndexPath)
    {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:prevIndexPath];
        cell.textLabel.textColor = [UIColor white];
    }
    NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
    if (nextIndexPath)
    {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
    cell.textLabel.textColor = [UIColor blackColor];
    } 
}

请访问此Apple docs了解更多详情

这是 swift 2.0 中的一个很好的解决方案。

   func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        if ((context.previouslyFocusedIndexPath) != nil) {
            let cell = tableView.cellForRowAtIndexPath(context.previouslyFocusedIndexPath!)
            cell?.textLabel?.textColor = UIColor.whiteColor()
        }
        if ((context.nextFocusedIndexPath) != nil) {
            let cell = tableView.cellForRowAtIndexPath(context.nextFocusedIndexPath!)
            cell?.textLabel?.textColor = UIColor.blackColor()
        }
    }

这是我使用 Swift 2.0 的方法:)

func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    if let prevIndexPath = context.previouslyFocusedIndexPath { 
        let prevCell = tableView.cellForRowAtIndexPath(prevIndexPath)
        prevCell?.backgroundColor = UIColor.blackColor()
    }

    if let nextIndexPath = context.nextFocusedIndexPath {
        let nextCell = tableView.cellForRowAtIndexPath(nextIndexPath)
        nextCell?.backgroundColor = UIColor.whiteColor()
    }
}

这是您想在 Swift 5.2 中实现的目标:

func tableView(_ tableView: UITableView, didUpdateFocusIn context: UITableViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if let prevIndexPath = context.previouslyFocusedIndexPath {
        let prevCell = tableView.cellForRow(at: prevIndexPath) as! TVHomeCell
        prevCell.titleLabel.font = .systemFont(ofSize: 32, weight: .medium)
    }

    if let nextIndexPath = context.nextFocusedIndexPath {
        let nextCell = tableView.cellForRow(at: nextIndexPath) as! TVHomeCell
        nextCell.titleLabel.font = .systemFont(ofSize: 34, weight: .bold)
    }
}

覆盖 func didUpdateFocus(上下文:UIFocusUpdateContext,协调器:UIFocusAnimationCoordinator){

    if let prevIndex = context.previouslyFocusedItem{
        
        if let myCell = prevIndex as? Mycell {
            
            myCell.textLabel.textColor = UIColor.white
            
        }
    }
    
    if let nextIndex = context.nextFocusedItem {
        if let myCell = nextIndex as? MyCell {
            
            myCell.citname.textColor = UIColor.black
        }
        
    }
}