我已经创建了一个表,其中单元格的大小会根据填充它们的内容量而动态变化。我遇到的问题是,当我向下滚动然后再次备份时,第一个单元格添加了不应该存在的内容。
Here是一个显示我的问题的视频。
以下是我用来尝试实现此功能的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
noteBody = [Noifications objectAtIndex:indexPath.row];
yPosition = 10;
NSLog(@"noteBody: %@",noteBody);
double titleHeight = 0;
NSUInteger titleLenght = [noteBody objectForKey:@"title"];
if (titleLenght < 30)titleHeight = 25;
else if(titleLenght < 60)titleHeight = 50;
else if(titleLenght < 90)titleHeight = 75;
else if(titleLenght < 120)titleHeight = 100;
else if(titleLenght < 150)titleHeight = 125;
else if(titleLenght < 180)titleHeight = 150;
else titleHeight = 200;
UILabel *Title = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, titleHeight)];
Title.text = [noteBody objectForKey:@"title"];
Title.font = [UIFont boldSystemFontOfSize:20];
Title.textAlignment = NSTextAlignmentCenter;
Title.numberOfLines = 0;
[Title sizeToFit];
yPosition += Title.frame.size.height;
[cell addSubview:Title];
UILabel *Date = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, 25)];
Date.text = [noteBody objectForKey:@"date"];
Date.numberOfLines = 0;
[Title sizeToFit];
yPosition += Date.frame.size.height;
[cell addSubview:Date];
double bodyHeight = 0;
NSUInteger bodyLenght = [noteBody objectForKey:@"title"];
if (bodyLenght < 30)titleHeight = 25;
else if(bodyHeight < 60)titleHeight = 50;
else if(bodyHeight < 90)titleHeight = 75;
else if(bodyHeight < 120)titleHeight = 100;
else if(bodyHeight < 150)titleHeight = 125;
else if(bodyHeight < 180)titleHeight = 150;
else if(bodyHeight < 210)titleHeight = 175;
else if(bodyHeight < 240)titleHeight = 200;
else if(bodyHeight < 270)titleHeight = 225;
else if(bodyHeight < 300)titleHeight = 250;
else if(bodyHeight < 330)titleHeight = 275;
else if(bodyHeight < 360)titleHeight = 300;
else if(bodyHeight < 390)titleHeight = 325;
else bodyHeight = 350;
UILabel *Body = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, bodyHeight)];
Body.text = [noteBody objectForKey:@"body"];
Body.numberOfLines = 0;
[Body sizeToFit];
yPosition += Body.frame.size.height;
[cell addSubview:Body];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
noteBodyCellHeight = [Noifications objectAtIndex:indexPath.row];
NSLog(@"cellHeights: %@",cellHeights);
NSString *title = [noteBodyCellHeight objectForKey:@"title"];
NSString *date = [noteBodyCellHeight objectForKey:@"date"];
NSString *body = [noteBodyCellHeight objectForKey:@"body"];
CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;
int section = indexPath.section;
CGSize title_size = {0, 0};
CGSize date_size = {0, 0};
CGSize body_size = {0, 0};
if (title && [title isEqualToString:@""] == NO ) {
title_size = [title sizeWithFont:[UIFont systemFontOfSize:22.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
if (date && [date isEqualToString:@""] == NO ) {
date_size = [date sizeWithFont:[UIFont systemFontOfSize:18.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
if (body && [body isEqualToString:@""] == NO ) {
body_size = [body sizeWithFont:[UIFont systemFontOfSize:18.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
CGFloat title_height = title_size.height;
CGFloat date_height = date_size.height;
CGFloat body_height = body_size.height;
//CGFloat content_size = Body.frame.size.height + Title.frame.size.height + Date.frame.size.height;
CGFloat content_size = title_height + date_height + body_height +20;
NSLog(@"content_size: %f",content_size);
CGFloat height;
switch ( section ) {
case 0:
height = content_size;
break;
//Just in case
default:
height = 44.0;
break;
}
return height;
}
有人可以告诉我哪里出错了,并指出我如何解决这个问题?
答案 0 :(得分:3)
尝试使用自定义单元格类或在重用时删除单元格现有子视图。
基本上每次调用cellForRow
时,子视图都会被添加到单元格中,即使它正被重复使用。
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
在上面的
之后加上这个for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperView];
}
也将cell addSubview
更改为cell.contentView addSubview
答案 1 :(得分:1)
您可能只想将三个子视图添加到单元格一次,为每个子视图添加一个唯一标记,即在if
块中初始化后。之后,您可以使用单元格上的-viewWithTag:
来引用标签并设置其大小和内容。