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

时间:2015-10-02 23:37:48

标签: objective-c uitableview tvos

如何更改"聚焦"的字体颜色? 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]];
 }

4 个答案:

答案 0 :(得分:9)

TVOS UITableViewDelegate中有新方法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了解更多详情

答案 1 :(得分:2)

这是我使用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()
    }
}

答案 2 :(得分:0)

这是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()
        }
    }

答案 3 :(得分:0)

这是您要在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)
    }
}