2015-10-02 2 views
4

Comment changer la couleur de police d'une cellule UITableView "focalisée"? J'ai actuellement ce ....tableview, changez la couleur de police de focus tvos

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

Je sais que si vous voulez changer un arrière-plan d'un concentré UITableViewCell serait:

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

La question est au-dessus que vous créez de nouvelles cellules (dequeueReusableCellWithIdentifier), ne pas modifier les cellules existantes. Voir la réponse ci-dessous à propos de l'utilisation de cellForRowAtIndexPath. –

Répondre

8

Dans TVOSUITableViewDelegate ont nouvelle méthode tableView:didUpdateFocusInContext:withAnimationCoordinator: qui sera appelé lorsque le changement de mise au point, vous pouvez obtenir indexPath précédent et nextFocusIndexPath et vous pouvez utiliser des méthodes tableView pour obtenir des cellules, votre code devrait ressembler à ceci

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

S'il vous plaît visitez ce Apple docs pour plus de détails

+0

j'ai essayé ceci et seulement la première cellule change la couleur de police mais si je fais défiler et concentre sur une autre cellule, la couleur de texte ne change pas –

+1

j'ai le code de mise à jour, et ce code j'utilise et travaille pour moi –

+0

merci! !!!! TU ES LE MEILLEUR! Cela a très bien fonctionné! –

0

Voici une bonne solution dans 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

Vous devriez vous renseigner sur l'idiome 'if let foo = bar' dans Swift. Chaque fois que vous faites quelque chose comme 'context.previouslyFocusedIndexPath!' (Je veux dire le '!' Ici), vous êtes probablement en train de faire quelque chose de non-swifty :-) – DarkDust

2

Voici mon approche à l'aide 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() 
    } 
} 
+0

Cela ne fonctionne pas correctement si 'nextFocusedIndexPath' n'est pas nul, alors que le précédent est "nul". – Mazyod

+0

done :) réponse mise à jour afin qu'elle prenne en compte tous les scénarios –