我有一个UITableView,它不会释放它在屏幕滚动后显示的数据。
每个表格单元格都需要显示名称,日期和图片。第一个完整的屏幕单元在启动时正常工作,因为它们没有被重用。然而,一旦你开始滚动并重新使用一个单元格,显示的数据将堆叠在该单元格之前存储的内容上(例如:日期将于1981年7月4日堆叠在1972年12月15日之上)。
这是我的代码:
- (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;
}
答案 0 :(得分:1)
不确定这是否是最佳解决方案,但这对我有用。当我需要将UI字段添加到单元格子视图时,单元格似乎并不总是nil要解决此问题,我首先检查是否设置了任何单元格标记。如果不是,我将所有UI字段添加回单元格子视图。
- (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;
}
答案 1 :(得分:0)
messageFeed:每次在tableview中分配视图,当在上一个Feed视图中添加的tableview feedView中重新使用单元格时,请尝试使用此可重用代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
// Configure the cell...
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UIView *feedView = [[UIView alloc] init];
feedView.tag=1;
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(..., ..., ..., ...)];
nameLabel.tag=2;
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(..., ..., ..., ...)];
dateLabel.tag=3;
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(..., ..., ..., ...)];
imageView.tag=4;
[feedView addSubview:nameLabel];
[feedView addSubview:dateLabel];
[feedView addSubview:imageView];
[cell addSubview:feedView];
}
UIView *feedView=(UIView*)[cell viewWithTag:1];
UILabel *nameLabel =(UILabel*)[feedView viewWithTag:2];
UILabel *dateLabel =(UILabel*)[feedView viewWithTag:3];
UIImageView *imageView =(UIImageView*)[feedView viewWithTag:4];
nameLabel.text = self.nameArray[indexPath.row];
dateLabel.text = self.dateArray[indexPath.row];
imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]];
return cell;
}