Reputation: 716
Sometimes i am not getting correct UITableViewCell height. My table cell contains three labels as i have mentioned in the attached screenshot. please check my below code and help me to fix this issue.
here is my code :
-(CGFloat)getTextHeightForIndex:(NSInteger)index
{
CGSize maximumSize = CGSizeMake(242, 10000);
Agenda *agenda = [[Agenda alloc] init];
agenda = [arrayMyAgenda objectAtIndex:index];
NSString *strTotalText = [NSString stringWithFormat:@"%@ %@ %@", agenda.program_name, agenda.program_hall, [NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time]];
CGSize labelHeightSize = [strTotalText sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
return labelHeightSize.height +20;
}
#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrayMyAgenda count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self getTextHeightForIndex:indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Agenda *myAgenda = [[Agenda alloc] init];
myAgenda = [self.arrayMyAgenda objectAtIndex:indexPath.row];
NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UILabel *lblTitleText = nil;
UILabel *lblAreaText = nil;
UILabel *lblTimingText = nil;
//First label for Title
lblTitleText = [[UILabel alloc] initWithFrame:CGRectMake(10, 6, 305, 24)];
lblTitleText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
lblTitleText.backgroundColor = [UIColor clearColor];
lblTitleText.text = myAgenda.program_name;
lblTitleText.textAlignment = NSTextAlignmentLeft;
lblTitleText.numberOfLines = 0;
lblTitleText.tag = 10;
[lblTitleText sizeToFit];
lblTitleText.textColor = [UIColor colorWithRed:(59/255.0) green:(59/255.0) blue:(59/255.0) alpha:1.0];
[cell.contentView addSubview:lblTitleText];
//Second label for Area name
lblAreaText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + 4, 305, 21)];
lblAreaText.text = myAgenda.program_hall;
lblAreaText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
lblAreaText.backgroundColor = [UIColor clearColor];
lblAreaText.tag = 20;
lblAreaText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0];
lblAreaText.textAlignment = NSTextAlignmentLeft;
lblAreaText.numberOfLines = 0;
[lblAreaText sizeToFit];
[cell.contentView addSubview:lblAreaText];
//Third label for timings
lblTimingText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + lblAreaText.frame.size.height + 2, 305, 21)];
lblTimingText.text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time];
lblTimingText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
lblTimingText.backgroundColor = [UIColor clearColor];
lblTimingText.tag = 30;
lblTimingText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0];
[cell.contentView addSubview:lblTimingText];
return cell;
}
else
{
((UILabel *)[cell.contentView viewWithTag:10]).text = myAgenda.program_name;
((UILabel *)[cell.contentView viewWithTag:20]).text = myAgenda.program_hall;
((UILabel *)[cell.contentView viewWithTag:30]).text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"middleRowSelected.png"]];
cell.selectedBackgroundView = selectionColor;
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"recentchatcell.jpg"]];
return cell;
}
}
Thanks in advance.
Upvotes: 0
Views: 179
Reputation: 2343
I think the problem is you get the height of combined strings that will display into three different labels. You should get each string's height that will go to each label and add them to get the cell height. Doesn't it make more sense getting height of each labels from the string that will display on them and add them together to get the cell height?
Try this:
-(CGFloat)getTextHeightForIndex:(NSInteger)index
{
CGSize maximumSize = CGSizeMake(305, 10000);
Agenda *agenda = [[Agenda alloc] init];
agenda = [arrayMyAgenda objectAtIndex:index];
CGFloat cellHeight = 0;
CGSize titleLabelHeightSize = [agenda.program_name sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += titleLabelHeightSize.height > 24 ? titleLabelHeightSize.height : 24;
CGSize areaLabelHeightSize = [agenda.program_hal sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += areaLabelHeightSize.height > 21 ? areaLabelHeightSize.height : 21;
CGSize timinglLabelHeightSize = [[NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time] sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += timinglLabelHeightSize.height > 21 ? timinglLabelHeightSize.height : 21;
return cellHeight +20;
}
You can generalize this function more but I think this is good enough for your need.
Upvotes: 0
Reputation: 1355
Try this in your -(CGFloat)getTextHeightForIndex:(NSInteger)index
: instead of CGSize
use CGRect
. Worked for me.
CGRect labelHeightSize = [strTotalText boundingRectWithSize:CGSizeMake(280.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:16.0f]} context:nil];
return ceilf(labelHeightSize.size.height) + 50.0f;
Upvotes: 1