UItableviewcell with muliple labels and custom height
I have added multiple labels in My Tableview cell. I am displaying facebook message and sender name and photos.For some posts it might be possible that message is unavailable or photo is unavailable .. I checking null condition and if there is no value present I am not creating label for that particular cell... now some label has large string , some may have small.. I am confused with cell's height ,, I am able to get dynamic height for 1 label like this in example dynamic height but how can I manage Height according to number of label's text... my code is like
if ([(Facebook * )[tableArray objectAtIndex:indexPath.row]sender]!= nil) {
labelSender = [[UILabel alloc]initWithFrame:CGRectMake(image_view.frame.size.width+20, 20, 120, 20) ];
labelSender.text = [NSString stringWithFormat:@"%@",[(Facebook * )[tabl开发者_运维知识库eArray objectAtIndex:indexPath.row]sender]];
[labelSender setLineBreakMode:UILineBreakModeWordWrap];
[labelSender setNumberOfLines:0];
labelSender.textColor = [UIColor colorWithRed:59/255.0 green:89/255.0 blue:153/255.0 alpha:1.0];
labelSender.font = [UIFont fontWithName:@"Arial" size:15.0];
[cell.contentView addSubview:labelSender];
[labelSender release];
}
if ([(Facebook * )[tableArray objectAtIndex:indexPath.row]post]!= nil) {
NSLog(@"post is ==%@",[(Facebook * )[tableArray objectAtIndex:indexPath.row]post]);
labelMessage = [[UILabel alloc]initWithFrame:CGRectMake(image_view.frame.size.width+20, labelSender.frame.size.height + 20, 200, 20)];
labelMessage.text = [NSString stringWithFormat:@"%@",[(Facebook * )[tableArray objectAtIndex:indexPath.row]post]];
[labelMessage setLineBreakMode:UILineBreakModeWordWrap];
[labelMessage setNumberOfLines:0];
labelMessage.backgroundColor = [UIColor yellowColor];
labelMessage.font = [UIFont fontWithName:@"Arial" size:13.0];
[cell.contentView addSubview:labelMessage];
[labelMessage release];
}
please help
I feel its better to go for UITextView whenever handling dynamic content.
You should store the displayed labels in an NSMutableArray
that is a property of the cell. That way you can iterate through simply by using:
for (UILabel *tmpLabel in yourLabelArray){
// do: add height to sum
}
This will save you an the if (labelExists) for every label.
Good luck!
精彩评论