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.