2010-05-27 8 views
1

J'ai un UITableView qui réutilise des cellules lorsque l'utilisateur fait défiler. Tout apparaît et défile bien, sauf lorsque l'utilisateur clique sur une ligne réelle, la cellule en surbrillance affiche du texte d'une autre cellule. Je ne suis pas exactement sûr pourquoi.Problème lors de la réutilisation de UITeableViewCell

#define IMAGE_TAG 1111 
#define LOGIN_TAG 2222 
#define FULL_NAME_TAG 3333 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    STUser *mySTUser = [[[STUser alloc]init]autorelease]; 
    mySTUser = [items objectAtIndex:indexPath.row]; 

    AsyncImageView* asyncImage = nil; 
    UILabel* loginLabel = nil; 
    UILabel* fullNameLabel = nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    else { 
     asyncImage = (AsyncImageView *) [cell.contentView viewWithTag:IMAGE_TAG]; 
     loginLabel = (UILabel *) [cell.contentView viewWithTag:LOGIN_TAG]; 
     fullNameLabel = (UILabel *) [cell.contentView viewWithTag:FULL_NAME_TAG]; 
    } 

    // Configure the cell... 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    CGRect frame = CGRectMake(0, 0, 44, 44); 
    asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease]; 
    asyncImage.tag = IMAGE_TAG; 
    NSURL* url = [NSURL URLWithString:mySTUser.avatar_url_large]; 
    [asyncImage loadImageFromURL:url]; 
    [cell.contentView addSubview:asyncImage]; 

    loginLabel.tag = LOGIN_TAG; 
    CGRect loginLabelFrame = CGRectMake(60, 0, 200, 10); 
    loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 
    loginLabel.text = [NSString stringWithFormat:@"%@",mySTUser.login]; 
    [cell.contentView addSubview:loginLabel]; 

    fullNameLabel.tag = FULL_NAME_TAG; 
    CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 10); 
    fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease]; 
    fullNameLabel.text = [NSString stringWithFormat:@"%@ %@",mySTUser.first_name, mySTUser.last_name]; //[NSString stringWithFormat:@"%@",mySTUser.login]; 
    [cell.contentView addSubview:fullNameLabel];  


    return cell; 
} 

Répondre

6

Cette ligne alloue un objet puis le supprime. N'allouez pas un article que vous n'utiliserez pas.

STUser *mySTUser = [[[STUser alloc]init]autorelease]; 
mySTUser = [items objectAtIndex:indexPath.row]; 

À la place, déclarez simplement une variable et utilisez-la.

STUser *mySTUser; 
mySTUser = [items objectAtIndex:indexPath.row]; 

Cette ligne crée un nouvel objet et l'affecte à la cellule, mais cela se produit chaque fois que la cellule est utilisée.

asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease]; 
[cell.contentView addSubview:asyncImage]; 

À la place, placez toutes les lignes addSubview dans la condition if où la cellule est créée.

if (cell == nil) { 
    CGRect frame = CGRectMake(0, 0, 44, 44); 
    CGRect loginLabelFrame = CGRectMake(60, 0, 200, 10); 
    CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 10); 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease]; 
    [cell.contentView addSubview:asyncImage]; 
    loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 
    [cell.contentView addSubview:loginLabel]; 
    fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease]; 
    [cell.contentView addSubview:fullNameLabel];  
    asyncImage.tag = IMAGE_TAG; 
    loginLabel.tag = LOGIN_TAG; 
    fullNameLabel.tag = FULL_NAME_TAG; 
} else ... 

La seule chose qui devrait se produire à l'extérieur que si le bloc est assignations aux propriétés qui changent par cellule, comme [asyncImage loadImageFromURL:url]; et lui affecter le texte aux étiquettes.

Cette ligne affecte une propriété à un objet éventuellement nul, puis alloue l'objet.

loginLabel.tag = LOGIN_TAG; 
loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 

Attribuez plutôt la propriété après avoir créé l'objet.

loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 
loginLabel.tag = LOGIN_TAG; 

Cette ligne utilise une chaîne formatée où une affectation simple ferait l'affaire.

loginLabel.text = [NSString stringWithFormat:@"%@",mySTUser.login]; 

À la place, en supposant mySTUser.login, assignez-le directement.

loginLabel.text = mySTUser.login; 
0

Si la cellule n'est pas nulle, vous devez d'abord attribuer une image et marquer les variables de la cellule, puis les initialiser à nouveau. Droite?

Questions connexes