2

这简直让我大吃一惊。

我有一个自定义 UIView 和 UILable 作为子视图添加到 cell.contentView 中的 UITableView 中。我有一个名为 arryData 的数组,其中包含大约 100 个字符串作为我想在表中显示的数据。问题是,当创建表时,我看到 arryData 的前 0-4 行带有值,如果我继续向下滚动表,我看到的只是前 5 个字符串一遍又一遍地重复。我究竟做错了什么?这是我的代码。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"Inside cellForRowAtIndexPath");

    static NSString *CellIdentifier = @"Cell";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *labelValue1 = [[UILabel alloc] initWithFrame:CGRectMake(70, 25, 200, 30)];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
    {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        UIImageView *imgVw1 = [[UIImageView alloc] initWithFrame:CGRectMake(11, 0, 300, 75)];
        imgVw1.image = [UIImage imageNamed:@"background_pic5.png"];
        imgVw1.userInteractionEnabled = YES;
        imgVw1.exclusiveTouch = YES;
        [cell.contentView addSubview:imgVw1];


        labelValue1.text = [arryData objectAtIndex:indexPath.row];
        labelValue1.font = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size: 22.0];
        labelValue1.textColor = [UIColor whiteColor];
        labelValue1.textAlignment = UITextAlignmentCenter;
        labelValue1.numberOfLines = 1;
        labelValue1.backgroundColor = [UIColor clearColor];
        labelValue1.adjustsFontSizeToFitWidth = YES;
        labelValue1.minimumFontSize = 10.0;
        labelValue1.tag = (indexPath.row)+100;
        [cell.contentView addSubview:labelValue1];

        NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);
    }
    else
    {
        NSLog(@"indexPath.row: %d - arrayData: %@..." , indexPath.row, [arryData objectAtIndex:indexPath.row]);

        labelValue1 = (UILabel *) [cell viewWithTag:((indexPath.row)+100)];
        labelValue1.text = [arryData objectAtIndex:indexPath.row];
    }

    //Do this only if row has a value. For blank ones, Skip this


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        //its iphone
        cell.textLabel.font = [UIFont systemFontOfSize:13];
    }


    if([arryDataMutable containsObject:indexPath])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } 
    else 
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

    //cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

这就是当我向下滚动表格时我的输出的样子,我看到的只是我 labelValue1 中 indexPath.row 0 - 4 中的文本一遍又一遍地重复

indexPath.row: 0 - arrayData: Behind Whipped...
indexPath.row: 1 - arrayData: Aftershock Whip...
indexPath.row: 2 - arrayData: Bull Whipped Sound 1...
indexPath.row: 3 - arrayData: Bull Whipped Sound 2...
indexPath.row: 4 - arrayData: Bullet Fire Whip...
indexPath.row: 5 - arrayData: Circus Whip...
indexPath.row: 6 - arrayData: Circus Whip2...
4

1 回答 1

4

在这种情况下更改您设置标签的方式,

labelValue1.tag = 100;

并将其读作,

labelValue1 = (UILabel *) [cell viewWithTag:100];

由于您正在使单元格出列,因此它将尝试重用单元格,并在您为单元格分配内存时将标签添加到该单元格中。执行 else 部分时,您需要访问已添加为[cell viewWithTag:100];.

[cell viewWithTag:((indexPath.row)+100)];对于第一次不可见的大多数单元格将返回 nil。由于您正在重用单元格,因此您将再次看到相同的前 5 个可见单元格和相同的文本。在这种情况下,您试图将文本设置为nil对象。所以它不会有任何区别,因此你会看到相同的文本重复。

于 2012-12-25T00:21:45.810 回答