TableView重用相同的单元格

时间:2011-05-22 23:26:59

标签: objective-c uitableview

在我的UITableview中,我收到一个奇怪的错误。我有一个名为_cellTextArray的字典数组,该字典包含键“textLabel”,“detailTextLabel”和“cornerLabel”。但是我创建了三个UILabel,它们被添加到单元格的contentView中,并且它们的文本被设置为字典中的相应名称。我的问题是textLabels只检索字典中的前5个对象,并将它们用于每个单元格,而不是检索字典中单元格索引处的对象。

请你告诉我我做错了什么。我已经梳理了几次代码,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;
}

2 个答案:

答案 0 :(得分:1)

检查this page上的清单5-3 (iOS表格视图编程指南),看看它是如何正确完成的。

答案 1 :(得分:0)

您只在没有可重复使用的单元格时设置数据。一旦它们在那里,表视图将不会要求新的单元格。所以你的数据没有被设置。你正在明确地返回重用的单元格。您应该在该单元格中设置所有静态属性(不依赖于行数据),并将所有赋值放在if语句之外。在这种情况下,它们似乎是三个标签的setText:语句。

相关问题