gpt4 book ai didi

ios - dequeueReusableCellWithIdentifier 不释放数据

转载 作者:行者123 更新时间:2023-11-29 03:16:33 26 4
gpt4 key购买 nike

我有一个 UITableView,一旦它滚出屏幕,它就不会释放它显示的数据。

每个表格单元格都需要显示名称、日期和图片。第一个充满屏幕的单元格在启动时可以正常工作,因为它们没有被重复使用。然而,一旦您开始滚动并且单元格被重复使用,显示的数据就会堆叠在该单元格之前存储的内容上(例如:日期将在 1972 年 12 月 15 日之上堆叠 1981 年 7 月 4 日)。

这是我的代码:

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"";

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellIdentifier];

self.nameArray = @[@"list of names"];
self.dateArray = @[@"list of dates"];
self.imageArray = @[@"list of images"];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.nameArray count];
}


- (UIView *)messageFeed:(NSIndexPath *)indexPath
{
UIView *feedView = [[UIView alloc] init];
//...feedView attributes

UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(..., ..., ..., ...)];
nameLabel.text = self.nameArray[indexPath.row];
//nameLabel attributes

UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(..., ..., ..., ...)];
dateLabel.text = self.dateArray[indexPath.row];
//dateLabel attributes

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(..., ..., ..., ...)];
imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]];
//imageView attributes

[feedView addSubview:nameLabel];
[feedView addSubview:dateLabel];
[feedView addSubview:imageView];

return feedView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier forIndexPath:indexPath];

// Configure the cell...

[cell addSubview:[self messageFeed:indexPath]];

return cell;
}

最佳答案

不确定这是否是最佳解决方案,但这对我有用。当我需要将 UI 字段添加到单元格 subview 时,单元格似乎并不总是 nil 为了解决这个问题,我首先检查是否设置了任何单元格标签。如果不是,我将所有 UI 字段添加回单元格 subview 。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    
{
UILabel *background;
UILabel *nameLabel;
UILabel *dateLabel;
UIImageView *imageView;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier];

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

background = (UILabel *) [cell viewWithTag:1];
if (!background) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
background = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
background.backgroundColor = [UIColor lightGrayColor];
background.tag = 1;
[cell.contentView addSubview:background];

nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(84.0f, 10.0f, 216.0f, 29.0f)];
nameLabel.textColor = [UIColor blackColor];
nameLabel.font = [UIFont boldSystemFontOfSize:20.0f];
nameLabel.tag = 2;
[cell.contentView addSubview:nameLabel];

dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(84.0f, 40.0f, 216.0f, 21.0f)];
dateLabel.textColor = [UIColor blackColor];
dateLabel.font = [UIFont systemFontOfSize:10.0f];
dateLabel.tag = 3;
[cell.contentView addSubview:dateLabel];

imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20.0f, 13.0f, 56.0f, 51.0f)];
imageView.tag = 4;
[cell addSubview:imageView];
} else {
nameLabel = (UILabel *) [cell viewWithTag:2];
dateLabel = (UILabel *) [cell viewWithTag:3];
imageView = (UIImageView *) [cell viewWithTag:4];
}

nameLabel.text = self.nameArray[indexPath.row];
dateLabel.text = self.dateArray[indexPath.row];
imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]];

return cell;

}

关于ios - dequeueReusableCellWithIdentifier 不释放数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618109/

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