Tableview,改变焦点的字体颜色



如何更改"焦点"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]];
 }

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文档

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

您可以通过使用上下文来更改颜色,它包含先前聚焦的单元格和当前聚焦的单元格的引用。只需将其转换为单元格类型,然后执行颜色更改操作。

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.textLabel.textColor = UIColor.black
        }
        
    }
}

最新更新