gpt4 book ai didi

ios - 为什么旧值出现在tableViewCell中

转载 作者:行者123 更新时间:2023-11-29 04:42:21 25 4
gpt4 key购买 nike

我在制作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;

最佳答案

问题是您每次调用单元格时都会添加图像和标签 subview 。相反,您只想在创建单元格时添加这些 subview 。每次调用单元格时,您只想设置 subview 的值。你会想要这样的东西(凭内存完成):

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;

关于ios - 为什么旧值出现在tableViewCell中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com