0

我在制作 TableViewCell 时添加了一个 UILabel。像这样的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Special *special = [speciales objectAtIndex:indexPath.row];
    ......
    UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
    description.text = special.specialDescription;
    description.font = [UIFont fontWithName:@"Heiti SC" size:12];
    description.textColor = [UIColor darkGrayColor];
    description.lineBreakMode = UILineBreakModeWordWrap;
    description.numberOfLines = 3;

    [cell addSubview:description];

    return cell;
}

它工作得很好,但是当我从下到上滚动它时,当我选择一行时,旧值同时出现。谁能帮我解决这个问题?

谢谢!

更新:我所有的代码都是这样的: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
img.image = special.specialIconImage;

[self addShadowToImage:img];

[cell addSubview:img];

UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.text = special.specialName;

name.font = [UIFont fontWithName:@"Heiti SC" size:16];

[cell addSubview:name];

UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
description.text = special.specialDescription;
description.font = [UIFont fontWithName:@"Heiti SC" size:12];
description.textColor = [UIColor darkGrayColor];
description.lineBreakMode = UILineBreakModeWordWrap;
description.numberOfLines = 3;

[cell addSubview:description];

return cell;

}

4

5 回答 5

1

问题是每次调用单元格时您都在添加 Image 和 Label 子视图。相反,您只想在创建单元格时添加这些子视图。每次调用单元格时,您只想设置子视图的值。你会想要这样的东西(完成记忆):

Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
    [imageView setTag:100];
    [cell addSubview:imageView];

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
    name.font = [UIFont fontWithName:@"Heiti SC" size:16];
    [nameLabel setTag:101];
    [cell addSubview:nameLabel];

    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
    [descriptionLabel setTag:102];
    descriptionLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
    descriptionLabel.textColor = [UIColor darkGrayColor];
    descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    descriptionLabel.numberOfLines = 3;
    [cell addSubview:descriptionLabel];
}

UIImageView *img = (UIImageView *)[cell viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];

UILabel *name = (UILabel *)[cell viewWithTag:101];
name.text = special.specialName;

UILabel *description = (UILabel *)[cell viewWithTag:102];
description.text = special.specialDescription;

return cell;
于 2012-04-17T16:27:18.217 回答
0

[tableView 重载数据]; 尝试这个。

于 2012-04-17T11:01:38.137 回答
0

代码中的......是什么意思?

你在那里使用 dequeueReusableCellWithIdentifier 吗?

如果那部分代码包含如下代码,那么它应该可以正常工作-

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
于 2012-04-17T11:09:35.027 回答
0

虽然我认为您应该将单元格出列以重用它们,但这似乎不是您问题的根源 - 除非您正在重用单元格并且没有向我们展示。那里可能有指向问题的代码。

我认为这个问题可能与重绘有关。尝试将其放在tableView:cellForRowAtIndex:方法的末尾

[cell setNeedsDisplay];

这将强制 UI 重绘单元格。

于 2012-04-17T11:22:33.513 回答
0

以下是一些需要注意的事项:

  • The new views must be added when we instantiate a new cell, but not when we
    reuse a cell (because a reused cell already has them).

  • We must never send addSubview: to the cell itself — only to its contentView (or
    some subview thereof).

  • We should assign the new views an appropriate autoresizingMask, because the
    cell’s content view might be resized.

  • Each new view should be assigned a tag so that it can be referred to elsewhere.   

这是代码:

//thanks to Thomas Hajcak's code
Special *special = [speciales objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
[imageView setTag:100];


//autoresizingMask as follow
imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeight |
                           UIViewAutoresizingFlexibleLeftMargin);
[cell.contentView addSubview:imageView];

UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.font = [UIFont fontWithName:@"Heiti SC" size:16];
[nameLabel setTag:101];

[cell.contentView addSubview:nameLabel];

UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
[descriptionLabel setTag:102];
descriptionLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
descriptionLabel.textColor = [UIColor darkGrayColor];
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
descriptionLabel.numberOfLines = 3;
[cell.contentView addSubview:descriptionLabel];
}

UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];

UILabel *name = (UILabel *)[cell.contentView viewWithTag:101];
name.text = special.specialName;

UILabel *description = (UILabel *)[cell.contentView viewWithTag:102];
description.text = special.specialDescription;

return cell;
于 2012-04-18T05:47:19.737 回答