2011-05-22 55 views
0

在我的UITableview中,我收到一個奇怪的錯誤。我有一個名爲_cellTextArray的字典數組,該字典包含鍵「textLabel」,「detailTextLabel」和「cornerLabel」。但是,我創建了三個UILabel,它們被添加到單元格的contentView中,並且它們的文本在字典中被設置爲相應的名稱。我的問題是,textLabels只檢索字典中的前5個對象,並將它們用於每個單元格,而不是在字典中的單元格的索引處檢索對象。TableView重用相同的單元格

請你能告訴我我做錯了什麼。我已經梳理了幾遍代碼,NSLog爲每個indexPath的dictionaryValues,它們都是正確的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    CheckBox *btn = (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     [cell.contentView addSubview:btn]; 
     //I added this code: 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 3; // 0 means no max. 


     UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient7.png"]] autorelease]; 
     [cell setBackgroundView:img]; 

     UILabel *lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease]; 
     [lab setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"textLabel"]]; 
     [lab setBackgroundColor:[UIColor clearColor]]; 
     [lab setTextColor:[UIColor whiteColor]]; 
     [lab setAdjustsFontSizeToFitWidth:YES]; 
     [lab setTextAlignment:UITextAlignmentLeft]; 
     [cell.contentView addSubview:lab]; 

     UILabel *dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease]; 
     [dlabl setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"detailTextLabel"]]; 
     [dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]]; 
     [dlabl setBackgroundColor:[UIColor clearColor]]; 
     // [dlabl setAdjustsFontSizeToFitWidth:YES]; 
     [dlabl setTextAlignment:UITextAlignmentLeft]; 
     [dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]]; 
     [cell.contentView addSubview:dlabl]; 

     UILabel *cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease]; 
     [cornerLabel setTextColor:[UIColor whiteColor]]; 
     //[cornerLabel setFont:[UIFont systemFontOfSize:12]]; 
     [cornerLabel setAdjustsFontSizeToFitWidth:YES]; 
     [cornerLabel setBackgroundColor:[UIColor clearColor]]; 
     [cornerLabel setTextAlignment:UITextAlignmentCenter]; 
     [cell.contentView addSubview:cornerLabel]; 
     [cornerLabel setAdjustsFontSizeToFitWidth:YES]; 
     [cornerLabel setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"cornerLabel"]]; 

     // Adds image to cell 
     // UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 140, 50)] autorelease]; 
    // [imageView setImage:[UIImage imageNamed:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"image"]]]; 
    // [cell.contentView addSubview:imageView];    
    } 
    // Configure the cell. 
    return cell; 
} 

回答

1

檢查清單5-3this page(iOS桌面視圖編程指南),看看它是如何正確完成的。

0

您只在沒有可重用單元時設置數據。一旦他們在那裏,表格視圖不會要求一個新的單元格。所以你的數據沒有被設置。你明白地返回重用的單元格。您應該在該單元格中設置所有靜態屬性(不依賴於行數據),並將所有賦值放在if語句之外。在這種情況下,它們似乎是三個標籤的setText:陳述。

相關問題