iphone - UITableView 和 UILabel 重复

标签 iphone ios ipad uitableview

这让我完全崩溃了。

我有一个自定义的 UIView 和 UILable,我已将它们作为 subview 添加到 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;

}

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

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...

最佳答案

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

labelValue1.tag = 100;

并将其解读为,

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

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

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

关于iphone - UITableView 和 UILabel 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026637/

相关文章:

iphone - 如何使用 Core Data 设计多标签功能?

iphone - 如何创建并应用我计算出的图像倾斜变换?

ipad - 如何在弹出框内显示键盘?,ipad 密码锁定样式

iphone - 由于 UIImageViews,应用程序运行缓慢

php - 使用 php 接收 XMPP 消息

iphone - 如何将 CCTransitionPageTurn 方向设置为垂直?

ios - clipsToBounds 似乎在 UIStackView 中没有效果

ios - UISplitViewController:如何从 Detail 返回到 Master

ios - 统一检查互联网连接可用性

ios - iOS 上的参数错误 : Error #2015: Invalid BitmapData.