【问题标题】:TableView Reusing the Same CellsTableView 重用相同的单元格
【发布时间】:2011-09-24 21:48:00
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c uitableview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-01
        相关资源
        最近更新 更多