2011-05-22 49 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:陈述。

相关问题