セルを埋めるコンテンツの量に応じてサイズが動的に変化するセルを含むテーブルを作成しました。私が抱えている問題は、下にスクロールしてからもう一度戻ると、最初のセルに本来あるべきではないコンテンツが追加されていることです。
ここに私の問題を示すビデオがあります。
これを試して実装するために使用したコードを次に示します。
- (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;
}
誰かが私が間違っている場所を教えて、これを修正する方法を教えてもらえますか?