Tableview, change tvos focus font color

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

 - (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 the background of a focused UITableViewCell, this will be:

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

A TVOS UITableViewDelegatenew method has appeared tableView:didUpdateFocusInContext:withAnimationCoordinator:that will be called when the focus changes, you can get the previous IndexPath and nextFocusIndexPath, and then you can use the tableView methods to get the 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];
    } 
}

See Apple docs for more details .

+8
source

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

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()
        }
    }
0

Source: https://habr.com/ru/post/1610051/


All Articles