2011-11-25 3 views
0

dans mon application iPhone J'utilise un UITableView avec des cellules personnalisées. Ces cellules contiennent des étiquettes différentes, représentant principalement du texte. Mais j'utilise l'une des étiquettes comme une sorte d'indicateur d'état, en lui donnant des couleurs différentes. Depuis que le défilement est devenu assez lent, j'ai voulu réutiliser les cellules. Faire cela me donne un bon coup de pouce de performance mais le "label d'état" est mal affiché. Les couleurs ne sont plus correctes pour les différentes cellules (Toutes les autres étiquettes sont correctes).iPhone - Réutilisation de TableViewCells

Quelqu'un at-il rencontré de tels problèmes et peut me donner un indice?

Edit: Le code pour la coutume TableCell

- (void)setIncident:(Incident *)_incident{ 
[self setSelectionStyle:UITableViewCellEditingStyleNone]; 
incident = _incident; 

streamNameLbl.text = incident.streamName; 
jobNameLbl.text = incident.jobName; 
workInProgressByLbl.text = incident.workInProgressBy; 
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init]; 
[tempFormatter setDateFormat:@"hh:mm"]; 
errorTimeLbl.text = [NSString stringWithFormat:@"%@", [tempFormatter stringFromDate:incident.errorTime]]; 

[tempFormatter setDateFormat:@"dd. MMM"]; 
plandateLbl.text = [NSString stringWithFormat:@"%@", [tempFormatter stringFromDate:incident.planDate]]; 

returnCodeLbl.text = [@"RC: " stringByAppendingString: incident.returnCode]; 
runNumberLbl.text = [@"Run: " stringByAppendingString: incident.runNumber]; 
severityLbl.text = incident.severity; 
restartStatusLblValue.text = incident.restartStatus; 

    NSString * color = incident.severityColor; 
    NSString * colorR = [color substringWithRange:NSMakeRange(1, 2)]; 
    NSString * colorG = [color substringWithRange:NSMakeRange(3, 2)]; 
    NSString * colorB = [color substringWithRange:NSMakeRange(5, 2)]; 

    unsigned intColorR = 0; 
    unsigned intColorG = 0; 
    unsigned intColorB = 0; 

    NSScanner *scanner = [NSScanner scannerWithString:colorR]; 
    [scanner scanHexInt:&intColorR]; 
    scanner = [NSScanner scannerWithString:colorG]; 
    [scanner scanHexInt:&intColorG]; 
    scanner = [NSScanner scannerWithString:colorB]; 
    [scanner scanHexInt:&intColorB]; 

    incidentStatusLbl.backgroundColor = [UIColor clearColor]; 

    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = incidentStatusLbl.bounds; 
    gradient.startPoint = CGPointMake(0, 0.5); 
    gradient.endPoint = CGPointMake(1, 0.5); 
    UIColor * startColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; 
    UIColor * endColor = [UIColor colorWithRed:intColorR/255.0 green:intColorG/255.0 blue:intColorB/255.0 alpha:1]; 
    gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil]; 

    [incidentStatusLbl.layer insertSublayer:gradient atIndex:0]; 
} 

cellForRowAtIndexPath dans le UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

incidentCell = (IncidentCell *)[tableView dequeueReusableCellWithIdentifier:@"IncidentCell"]; 
if (incidentCell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IncidentCell" owner:self options:nil]; 
    incidentCell = [nib objectAtIndex:0]; 
    NSLog(@"Loading cell from xib file"); 
    } 
else{ 
    NSLog(@"Reusing cell");  
    } 

NSMutableArray *sectionDetails = ((NSMutableArray *)[incidentDic objectForKey:[self.sortedSections objectAtIndex:[indexPath section]]]); 

Incident *incident = [sectionDetails objectAtIndex:[indexPath row]]; 

[incidentCell setIncident:incident]; 

return incidentCell; 
} 
+1

Montrez-nous du code. – rckoenes

Répondre

3

Comme vous réutilisez les cellules, lorsqu'une cellule est demandé à - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath il n'est pas rechargés de votre plume. Donc, si vous définissez la couleur de l'étiquette au noir dans la pointe par exemple, et que vous faites dans votre code

if (some_condition) { 
    cell.myLabel.textcolor= myStatusColor; 
} 

lorsqu'une étiquette a une couleur myStatusColor une fois, il gardera quand vous le réutiliser.

Ainsi, vous aurez à faire

if (some_condition) { 
    cell.myLabel.textcolor= myStatusColor; 
} else { 
    cell.myLabel.textcolor= [UIColor black]; 
} 

En fait, votre problème vient de [incidentStatusLbl.layer insertSublayer:gradient atIndex:0];

Si vous l'utilisez une fois, vous aurez un sous-couches, (appelons-le subLayer1). Lorsque vous réutiliserez votre cellule, vous ajouterez une autre sous-couche à l'index 0, BEHIND subLayer1. Vous voudrez peut-être supprimer votre ancienne sous-couche personnalisée avant.

+0

(Édité mon premier message) Ce que je ne comprends pas, pourquoi est-ce que cela fonctionne avec le texte des étiquettes, mais pas avec la couleur? Je suis assez inexpérimenté avec le développement de l'iphone. – Pathos

+0

Ok, je vois, je modifie ma réponse. – Zoleas

+0

Très bien, merci, ça a marché. Dommage que cette question ait abaissé ma réputation ^^ – Pathos

Questions connexes