当我重复使用时,如何完全清除单元格? [英] How do I clear a cell completely when I reuse it?

查看:88
本文介绍了当我重复使用时,如何完全清除单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用[table reloaddata];

When I call [table reloaddata];

使用新数据重新绘制单元格,但是我的UILabel因为它们是在旧的UILabel上绘制而搞砸了,所以它是一团糟。

The cells get redrawn with new data, but my UILabels get messed up because they are drawn over the old UILabels, so its a mess.

    static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];


if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];   
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    cell.contentView.backgroundColor = [UIColor clearColor];
}

是我的单元格的初始化。

Is my Init of the cell.

我添加像这样的UILabel

I add a UILabel like so

        UILabel *theDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 35,140, 20)];
    [theDateLabel setBackgroundColor:[UIColor clearColor]];
    [theDateLabel setTextColor:[UIColor lightGrayColor]];
    [theDateLabel setText:[dateFormatter stringFromDate:theDate]];
    [theDateLabel setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:15]];
    [cell addSubview:theDateLabel];
    [theDateLabel release];

单元格中还有一些标签,同样的事情。

There are a few more labels in the cell, same thing.

我想要发生的是旧标签从单元格中消失,以便它们不再可见。

What I would like to happen is that the old labels disappear from the cell so that they are no longer visible.

推荐答案

您不应将 theDateLabel 添加为 cell 的子视图。您应该将其添加为 cell.contentView 的子视图。

You should not add theDateLabel as a subview of cell. You should add it as a subview of cell.contentView.

正如yuji建议的那样,实现此目的的一种方法是使用每个自定义子视图的属性创建 UITableViewCell 的子类。这样,您可以轻松地到达重用单元格的日期标签,为新行设置文本。

As yuji suggests, one way to implement this is to create a subclass of UITableViewCell with a property for each custom subview. That way you can easily get to the date label of a reused cell to set its text for the new row.

另一种常见方法是使用 属性,每个 UIView 都有。例如:

Another common approach is to use the tag property that every UIView has. For example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";
    static const int DateLabelTag = 1;

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];   

        UILabel *theDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 35,140, 20)];
        theDateLabel.tag = DateLabelTag;
        theDateLabel.backgroundColor = [UIColor clearColor];
        theDateLabel.textColor = [UIColor lightGrayColor];
        theDateLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
        [cell.contentView addSubview:theDateLabel];
        [theDateLabel release];
    }

    NSDate *theDate = [self dateForRowAtIndexPath:indexPath];
    UILabel *theDateLabel = [cell.contentView viewWithTag:DateLabelTag];
    theDateLabel.text = [dateFormatter stringFromDate:theDate];

    return cell;
}

这篇关于当我重复使用时,如何完全清除单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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