2010-12-14 31 views
0

J'ai une table déroulante. Dans chaque cellule, je dessine 3 UILabels. Les premières cellules tirent bien. Mais en faisant défiler la table, les UILabels semblent dessiner sur les UILabels précédents. C'est comme les étiquettes dans la cellule ont déjà une vieille étiquette qui a été effacée. Je pourrais probablement corriger cela en dessinant une couleur de fond sur toute la cellule chaque redessiner, mais cela n'explique toujours pas ce problème étrange et pourquoi cela se produit.Table déroulante présentant un problème de redessin. Ne semble pas effacer

Quelqu'un sait pourquoi cela se produit et quelle pourrait être la solution?

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease]; 
} 

Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
[cell addSubview:teamName1Label]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
[cell addSubview:teamVersusLabel]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
[cell addSubview:teamName2Label]; 

return cell; 
} 

Merci beaucoup -Code

Répondre

4

Je pense que la meilleure solution à ce problème est de définir ces étiquettes dans le - (UITableViewCell *) reuseTableViewCellWithIdentifier: (NSString *) Identifiant withIndexPath: (NSIndexPath *) indexPath méthode

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
teamName1Label.tag = 1; 
[cell.contentView addSubview:teamName1Label]; 
[teamName1Label release]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
teamVersusLabel.tag = 2; 
[cell.contentView addSubview:teamVersusLabel]; 
[teamVersusLabel release]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
teamName2Label.tag = 3; 
[cell.contentView addSubview:teamName2Label]; 
[teamName2Label release]; 

return cell; 
} 

maintenant la méthode cellForRowAtIndexPath sera être--

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

    UITableViewCell *cell=nil; 

    static NSString *Identifier = @"Cell"; 
    cell = [theTableView dequeueReusableCellWithIdentifier:Identifier]; 
    if(cell == nil){ 
    cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath]; 
    } 

    Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1]; 
    label.text = aMatch.teamName1; 

    label = (UILabel *)[cell.contentView viewWithTag:2]; 
    label.text = @"V"; 

    label = (UILabel *)[cell.contentView viewWithTag:3]; 
    label.text = aMatch.teamName2; 

    return cell; 
    } 

Juste essayer ce code..Hope il aide :)

0

Ce problème est la réutilisation des cellules. Le code ajoute les étiquettes à la cellule chaque fois que cellForRowAtIndexPath est appelé (même si la cellule n'est pas nulle).

Lorsque vous faites défiler, une cellule précédente contenant déjà les étiquettes est chargée, puis le code ajoute de nouvelles étiquettes aux anciennes.

Créez et ajoutez uniquement les étiquettes à la cellule dans la section if (cell == nil). Lors de la création des étiquettes, définissez des étiquettes sur les étiquettes.

Ensuite, en dehors du if, récupérez les étiquettes en utilisant leur balise en utilisant viewWithTag et définissez leur texte.