Search code examples
objective-cuitableviewtvos

tableview, change font color of focus tvos


How do you change the font color of a "focused" UITableView cell? I currently have this....

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

I know that if you want to change a background of a focused UITableViewCell it would be:

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

Solution

  • In TVOS UITableViewDelegate have new method tableView:didUpdateFocusInContext:withAnimationCoordinator: which will be called when focus change, you can get previous IndexPath and nextFocusIndexPath and then you can use tableView methods to get cells, your code should look like this

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

    Please visit this Apple docs for more detail