【问题标题】:duplicate rows in tableview on uitableviewcelluitableviewcell上的tableview中的重复行
【发布时间】:2009-08-25 09:06:55
【问题描述】:

我发现了一些与我的问题相似但不完全相同的帖子。

在我的应用程序中,用户可以在几个 uitableviews 之间导航以深入了解所需的结果。当用户向前,然后向后,然后向前等时,很明显,行正在重新绘制/重写,并且文本变得越来越粗。

我发现在某些帖子中,这可能与我使用 cellforrowatindexpath 方法中的 uilable 创建行的方式有关。

我需要做些什么,以便每次用户在表格视图之间前进和后退时不会重新填充/重绘行吗?我是否需要在下面的代码中添加一些内容或在 viewwillappear 方法中添加一些内容(当前 viewwillappear 中有一个“reloaddata”,但似乎没有帮助)?

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f);
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
    label.text = [mapareaArray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:label];

    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
    bgView.borderColor = [UIColor clearColor];
    bgView.fillColor = [UIColor whiteColor];
    bgView.position = CustomCellBackgroundViewPositionSingle;
    cell.backgroundView = bgView;
    return cell;
}

【问题讨论】:

    标签: iphone uitableview uilabel


    【解决方案1】:

    您遇到的问题是由于这一行:

    [cell.contentView addSubview:label];
    

    您正在向表格单元格添加子视图,无论它是否是新单元格。如果它是一个旧的单元格(从可重用池中出列),那么您将向该单元格添加另一个子视图。

    相反,您应该标记 UILabel,然后使用标记定位它以修改该 UILabel 的内容。在 if(cell == nil) 块中添加(并设置其所有属性)并标记 UILabel:

    if(cell == nil) {
      // alloc and init the cell view...
    
      UILabel *label = [[[UILabel alloc] init] autorelease];
      label.tag = kMyTag; // define kMyTag in your header file using #define
      // and other label customizations
      [cell.contentView addSubview:label]; // addSubview here and only here
      ...
    }
    

    然后找到它:

    UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
    label.text = [mapareaArray objectAtIndex:indexPath.row];
    

    并且无需将其重新添加为 if(cell == nil) 块之外的子视图。子视图已经存在(这就是为什么重用单元格视图效率更高的原因,如果你做得正确,那就是;)。

    【讨论】:

    • 非常感谢您。大帮助。最后一个问题-我没有编程背景,所以不确定在头文件中使用#define 是什么意思。这样做的语法是什么?我以前见过它并试图将'#define kMyTag'放在标题中,但这不起作用......我假设你需要将kMyTag定义为一个值但不确定语法是什么做这个。你能帮忙吗?
    【解决方案2】:

    .h 文件 'define' 放在头文件顶部的#import 语句之后,并被设置为 0,因为我不知道如何定义它:

    #define kMyTag 0
    

    .m 文件 我已根据您的 cmets 更新了此部分,但是 a) 未填充该表,b) 当用户导航到下一个视图并返回此视图时,它失败并显示“发送到实例的无法识别的选择器”,以及 c )我不得不放入两个“返回单元”;条目,否则它会倒下。我认为我的东西顺序错误,可能没有正确初始化东西???

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
            UILabel *label = [[[UILabel alloc] init] autorelease];
            label.tag = kMyTag; // define kMyTag in your header file using #define
            label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
            label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f);
            label.textColor = [UIColor blackColor];
            label.backgroundColor = [UIColor clearColor];
            label.opaque = NO;
    
            CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
            bgView.borderColor = [UIColor clearColor];
            bgView.fillColor = [UIColor whiteColor];
            bgView.position = CustomCellBackgroundViewPositionSingle;
            cell.backgroundView = bgView;
    
            [cell.contentView addSubview:label]; // addSubview here and only here
            return cell;
        }
    UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
    label.text = [mapareaArray objectAtIndex:indexPath.row];
    return cell;
    }
    

    【讨论】:

    • 我不确定是否应该使用 0 作为 kMyTag 的值。尝试从 1 开始,因为 0 可能是所有内容的默认值。更改后,删除 if 块内的返回单元格。
    【解决方案3】:

    您应该为该特定单元格创建 UITableViewCell 的子类,然后应用逻辑来查看是否需要每次都将子视图添加到自身。此外,使用 UITableviewcell 的 prepareForReuse 并在此期间删除所有子视图,然后再应用您是否要向 Cell 添加 UILabel 的逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多