2010-05-27 32 views
1

我有一個UITableView在用戶滾動時重新使用單元格。除非用戶點擊實際行,否則所有內容都會顯示並滾動,但突出顯示的單元格會顯示來自其他單元格的某些文本。我不完全確定爲什麼。重複使用UITableViewCell的問題

#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; 
} 

回答

6

這條線分配一個對象,然後丟棄它。不要分配您不會使用的物品。

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

而是隻聲明一個變量並使用它。

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

這行創建一個新對象,並將其分配給小區,但它發生在每次使用的小區的時間。

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

相反,將所有addSubview行放在創建單元格的if條件中。

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 ... 

應外發生的唯一的事情是,如果塊分配到每個細胞是變化,如[asyncImage loadImageFromURL:url];和分配文本標籤的屬性。

此行將一個屬性分配給一個可能爲零的對象,然後分配該對象。

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

相反,在創建對象後分配屬性。

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

此行使用一個格式化的字符串,其中一個簡單的任務將執行。

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

取而代之,假設mySTUser.login,直接分配它。

loginLabel.text = mySTUser.login; 
0

如果小區不爲零,首先要將圖像和標籤從細胞增值經銷商,然後從頭再來初始化它們。對?