2012-04-19 3 views
2

J'ai mis en place un style de vue de table groupé personnalisé en utilisant le code suivant dans cellForRowAtIndexPath. Chaque cellule reçoit une nouvelle vue d'arrière-plan et ses coins sont arrondis en fonction de sa position dans la section. J'utilise ce style tout au long de mon application et dans diverses vues, j'utilise des animations pour ajouter et supprimer des lignes. (En utilisant [tableview insertRowsAtIndexPaths:], etc).Arrière-plans personnalisés UITableViewCell, quand recharger, etc.

Si la dernière rangée d'une section est insérée ou retirée, j'ai besoin de pouvoir recharger les coins de la vue d'arrière-plan des cellules. Comment puis-je faire cela sans appeler [tableview reloadData] ou même reloadCellsAtIndexPaths? Ces deux fonctions gâchent les animations d'insertion et de suppression de cellules. Y a-t-il quelque part où je peux mettre cette définition de coin de fond qui serait appelée au moment opportun? Comment le tableau par défaut groupé fait-il cela?

Désolé, ce n'est pas clair. Demandez des éclaircissements et je vais modifier. Merci! :)

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:[self styleForCellAtIndexPath:indexPath] 
             reuseIdentifier:cellIdentifier] autorelease]; 

     // Theme the cell 
      TableCellBackgroundView *backgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame]; 
      TableCellBackgroundView *selectedBackgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame]; 
      selectedBackgroundView.selectedStyle = YES; 
      // top row, without header 
      BOOL header = [self tableView:aTableView heightForHeaderInSection:indexPath.section]!=0; 
      if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) { 
       backgroundView.topRadius = 6; 
       selectedBackgroundView.topRadius = 6; 
      } 
      // bottom row 
      if (indexPath.row == [self tableView:aTableView numberOfRowsInSection:indexPath.section]-1) { 
       backgroundView.bottomRadius = 6; 
       selectedBackgroundView.bottomRadius = 6; 
      } 
      cell.backgroundView = backgroundView; 
      cell.selectedBackgroundView = selectedBackgroundView; 
      cell.contentView.backgroundColor = [UIColor clearColor]; 
      cell.backgroundColor = [UIColor clearColor]; 
     } 
    } 
+0

Existe-t-il un moyen propre de détecter dans les cellules la disposition ou l'aperçu des coins? Cela ne me dérange pas de sous-classer UITableViewCell, je ne peux pas penser à une façon dont cela aiderait .. – Dash

Répondre

0

Si vous avez une méthode que vous appelez pour l'animation personnalisée, lorsque l'animation est terminée, ou plutôt avant l'affichage de la nouvelle cellule, en utilisant une sous-classe de UITableViewCell faire un appel à une méthode nommé quelque chose comme - (void) fixCornersWithIndexPath: (NSIndexPath *) indexPath.

- (void) customAnimationForCellAtIndexPath: (NSIndexPath *) path { 
    //your animation things 
    CustomCell *cell = [self.tableView cellForRowAtIndexPath: indexPath]; 
    [cell fixCornersWithIndexPath: path andRowCount: [self tableView:aTableView numberOfRowsInSection:indexPath.section]]; 
} 

puis fixer les angles doivent être quelque chose comme ceci:

- (void) fixCornersWithIndexPath: (NSIndexPath *) indexPath andRowCount: (NSInteger *) rowCount { 
     if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) { 
      self.backgroundView.topRadius = 6; 
      self.selectedBackgroundView.topRadius = 6; 
     } 
     // bottom row 
     if (indexPath.row == rowCount-1) { 
      self.backgroundView.bottomRadius = 6; 
      self.selectedBackgroundView.bottomRadius = 6; 
     } 
} 

Hope this helps!

0

Je pense que c'est ce que vous cherchez. Une fois que vous avez modifié votre modèle, appelez:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

Cela entraînera votre méthode de cellAtIndexPath à invoquer. Envoyer un tableau avec un seul chemin d'index de la ligne qui est obsolète (je suppose que le problème des coins arrondis, soit la dernière ligne après la suppression d'une ligne, soit la ligne avant la dernière ligne ajoutée).

Vous pouvez l'appeler avec vos autres méthodes de mise à jour entre les mises à jour de début et de fin.

0

La solution qui a fonctionné pour moi est la suivante:

  1. Définir l'arrière-plan tableView: willDisplayCell: forRowAtIndexPath:
  2. Créer une méthode pratique appelée reloadBackgroundForRowAtIndexPath qui appelle willDisplayCell manuellement
  3. Quand j'ajouter une ligne, appelez la méthode pratique après un délai pour permettre aux animations de table de se terminer

    - (void)reloadBackgroundForRowAtIndexPath:(NSIndexPath*)indexPathToReload { 
        [self tableView:self.tableView willDisplayCell:[self.tableView cellForRowAtIndexPath:indexPathToReload] forRowAtIndexPath:indexPathToReload]; 
    } 
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
        // Logic to determine the background image 
    } 
    
    // elsewhere, update the table, then 
    NSIndexPath *indexPathToFix = [NSIndexPath indexPathForRow:count-1 inSection:0]; 
    // If the new row is coming out, we want to update the background right away. If the new row is going away, we want to wait to update until the animation is done. 
    // I don't like hard-coding the animation length here, but I don't think I have a choice. 
    [self performSelector:@selector(reloadBackgroundForRowAtIndexPath:) withObject:indexPathToFix afterDelay:(self.tableView.isEditing ? 0.0 : 0.2)]; 
    
Questions connexes