Reputation: 3044
I have created a Table with cells that size dynamically change depending on the amount of content that will be filling them. The problem that I am having is that when I scroll down then back up again The first cell has added content that should not be there.
Here is a video showing my problem.
Here is the code that I have used to try and implement this.
- (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;
}
Can Anyone tell me where I have gone wrong and point me in the direction how I could fix this?
Upvotes: 1
Views: 968
Reputation: 766
You probably want to add your three subviews to the cell only once, giving each a unique tag, namely after initializing it in the if
block. Afterwards, you can reference your labels and set their sizes and contents by using -viewWithTag:
on the cell.
Upvotes: 1
Reputation: 21805
try to use custom cell class or remove cell existing subviews in reuse.
Basically on each call of cellForRow
.. subviews are added to the cell even if it is being reused.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
add this after the above
for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperView];
}
also change cell addSubview
to cell.contentView addSubview
Upvotes: 3