2013-05-30 6 views
0

Après avoir ajouté une police et une ombre à certains UILabels, j'ai remarqué que l'animation de la vue table est retardée lorsque la vue est retirée de la pile (un glissement latéral comme FB/Path utilise). Le glissement latéral était lisse jusqu'à ce que j'ajoute les ombres UILabel.Comment optimiser cellForRowAtIndexPath: code

Je pense que je pourrais l'ajouter au mauvais endroit afin que les propriétés de l'étiquette soient ajoutées incorrectement peut-être. S'il vous plaît jeter un oeil à la méthode cellForRowAtIndexPath: ci-dessous:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
    imageView.image = [UIImage imageNamed:@"rest.jpg"]; 
    [cell.contentView addSubview:imageView]; 

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

    titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 

    titleLabel.textColor = [UIColor whiteColor]; 
    [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
    titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    titleLabel.layer.shadowOpacity = 0.7; 

    [cell.contentView addSubview:titleLabel]; 

    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 

    detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 
    detailLabel.backgroundColor = [UIColor clearColor]; 

    detailLabel.textColor = [UIColor whiteColor]; 
    [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
    detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    detailLabel.layer.shadowOpacity = 0.7; 

    [cell.contentView addSubview:detailLabel]; 

    cell.contentView.backgroundColor = [UIColor clearColor]; 


    return cell; 
} 

Merci pour toute aide.

+2

titleLabel.layer.shouldRasterize = OUI; detailLabel.layer.shouldRasterize = YES; – Amar

+0

@Amar merci! cela fait la différence – hanumanDev

+0

Mais aussi vérifier la réponse de @Wain !! –

Répondre

4

Vous ajoutez toujours de nouvelles vues. Ainsi, chaque fois que vous faites défiler la table, vos cellules reçoivent de plus en plus de contenu.

Créez toutes vos sous-vues lorsque la cellule est créée, puis mettez à jour les paramètres de sous-vues. Quelque chose comme:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
     imageView.tag = 123123; 
     [cell.contentView addSubview:imageView]; 

     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

     titleLabel.tag = 234234]; 
     titleLabel.backgroundColor = [UIColor clearColor]; 

     titleLabel.textColor = [UIColor whiteColor]; 
     [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
     titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     titleLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:titleLabel]; 

     UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 

     detailLabel.tag = 345345]; 
     detailLabel.backgroundColor = [UIColor clearColor]; 

     detailLabel.textColor = [UIColor whiteColor]; 
     [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
     detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     detailLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:detailLabel]; 

     cell.contentView.backgroundColor = [UIColor clearColor]; 
    } 

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123]; 
    imageView.image = [UIImage imageNamed:@"rest.jpg"]; 

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234]; 
    titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345]; 
    detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 

    return cell; 
}  
1

Comme les attributs de texte ne changent jamais, déplacez le code qui les définit dans l'instruction if. Conservez uniquement le code qui définit l'image et le texte de vos libellés en dehors de l'instruction if. Les cellules sont réutilisées, de sorte que les attributs tels que la police, etc. restent dans la cellule même après qu'elle a été "recyclée". Dans la branche else, ajoutez du code qui trouve des étiquettes existantes dans la cellule. Sinon, vous continuez d'ajouter la même étiquette à la cellule plusieurs fois.

1

Vous ajoutez des sous-vues, même après la file d'attente au lieu d'initialiser une nouvelle cellule. Assurez-vous que tout le code qui crée et ajoute des sous-vues est seulement fait lors de l'initialisation d'une cellule. Si vous devez vous référer aux vues de la cellule pour la configuration, sous-classe UITableViewCell.

De plus, le rendu d'ombre pourrait être ralentir trop, ajouter un Shadowpath pour rendre le rendu plus efficace:

ajouter à votre méthode tableView:cellForRowAtIndexPath::

... 
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath; 
detailLabel.layer.shadowPath = shadowPath; 
... 
0

Remplacez votre code avec ceci:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 


     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
     imageView.image = [UIImage imageNamed:@"rest.jpg"]; 
     imageView.tag =1; 
     [cell.contentView addSubview:imageView]; 

     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

     titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 
     titleLabel.backgroundColor = [UIColor clearColor]; 
     titleLabel.tag = 2; 
     titleLabel.textColor = [UIColor whiteColor]; 
     [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
     titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     titleLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:titleLabel]; 

     UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 
     detailLabel.tag = 3; 
     detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 
     detailLabel.backgroundColor = [UIColor clearColor]; 

     detailLabel.textColor = [UIColor whiteColor]; 
     [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
     detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     detailLabel.layer.shadowOpacity = 0.7; 


    } 

    UIImageView *tempImgView = (UIImageView *)[cell viewWithTag:1]; 
    tempImgView.image = [UIImage imageNamed:@""];// Here you can set any image by reusing imageview without allocating again and again 

    UILabel *tempLabel; 

    tempLabel = (UILabel *)[cell viewWithTag:2]; 
    tempLabel.text = @"";// Here you can access your title label and can set its properties without allocating again 

    tempLabel = (UILabel *)[cell viewWithTag:3]; 
    tempLabel.text = @"";// Here you can access your detailLabel label and can set its properties without allocating again 


    [cell.contentView addSubview:detailLabel]; 

    cell.contentView.backgroundColor = [UIColor clearColor]; 


    return cell; 
} 
0

Vous devez créer une cellule de vue de table personnalisée avec une classe. Ce code que vous avez ajouté de nombreux libellé puis shadow ne s'affiche pas correctement. code comme ça.

(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { NSString statique * cellReuseIdentifier = @ "cellReuseIdentifier"; UITableCustomViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellReuseIdentifier];

if (cell == nil) 
{ 
    cell = [[UITableCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 
} 

cell.imageView.image = [UIImage imageNamed:@"rest.jpg"]; 

cell.titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 

cell.detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 

return cell; 

}

1

Cet article de Twitter ingénierie vous donne un aperçu: http://engineering.twitter.com/2012/02/simple-strategies-for-smooth-animation.html

Fondamentalement, vous voulez éviter d'utiliser des sous-vues, mais plutôt attirer votre contenu directement avec Quartz. C'est la meilleure chose que vous pouvez faire pour améliorer les performances. Aussi: Évitez la transparence!Dans les instruments, vous pouvez également utiliser l'instrument Core Animation et activer « Couleur couches Blended » pour voir où les vues transparentes sont composées:

enter image description here