UITableView和UILabel重复 [英] UITableView and UILabel repeating

查看:90
本文介绍了UITableView和UILabel重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这完全扼杀了我。

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

I have a custom UIView and UILable that I have added to my UITableView in cell.contentView as a subview. I have an array called arryData with about 100 strings as data in it that I want to show in my table. The problem is that when table is created I see the first 0-4 rows from my arryData with values and if I keep scrolling down the table, all I see are the first 5 strings repeating over and over. What am I doing wrong? here is my code.

// 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重复一遍又一遍

This is what my output looks like when I scroll down the table and all I keep seeing is text in my labelValue1 from indexPath.row 0 - 4 repeat over and over

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


推荐答案

在这种情况下改变你设置标签的方式,

Change the way you are setting tag in this case,

labelValue1.tag = 100;

并将其读作,

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

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

Since you are dequeuing the cells, it will try to reuse the cell and label is added to that when you are allocating memory for the cell. When the else part is executed, you need to access the same label which was already added as [cell viewWithTag:100];.

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

[cell viewWithTag:((indexPath.row)+100)]; will return nil for most of the cells which were not visible for the first time. Since you are reusing cells, you will see the same first 5 visible cells again with same text. In this case, you were trying to set text to a nil object. So it wont make any difference and hence you will see the same text repeating.

这篇关于UITableView和UILabel重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆