【问题标题】:tableview, change font color of focus tvostableview,更改焦点 tvos 的字体颜色
【发布时间】:2015-12-31 06:19:41
【问题描述】:

如何更改“聚焦”的 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]];
 }

【问题讨论】:

  • 上述问题是您正在创建新单元格(dequeueReusableCellWithIdentifier),而不是修改现有单元格。请参阅下面有关使用 cellForRowAtIndexPath 的答案。

标签: objective-c uitableview tvos


【解决方案1】:

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: 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
        }
        
    }
}

【讨论】:

  • 欢迎来到 Stack Overflow。当代码附有解释时,它会更有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请编辑您的答案并解释它如何回答所提出的具体问题。见【如何回答】stackoverflow.com/questions/how-to-answer)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是 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 中的 if let foo = bar 成语。每当您执行context.previouslyFocusedIndexPath! 之类的操作(我的意思是这里的!)时,您很可能会做一些不迅速的事情:-)
    【解决方案4】:

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

    【讨论】:

    • 如果nextFocusedIndexPath 不为nil 则无法正常工作,而前一个为nil
    • 完成 :) 答案已更新,因此它考虑了所有场景
    【解决方案5】:

    TVOS UITableViewDelegate 有新方法tableView:didUpdateFocusInContext:withAnimationCoordinator: 会在焦点改变时调用,你可以获取previous 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了解更多详情

    【讨论】:

    • 我试过了,只有第一个单元格改变了字体颜色,但是如果我向下滚动并关注另一个单元格,文本颜色不会改变
    • 我有更新代码,这个代码我正在使用并为我工作
    • 谢谢!我将在下面发布我的快速解决方案
    猜你喜欢
    • 2016-01-22
    • 2011-09-23
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多