I have a grouped UITableView.
I am overriding the table row height, but I would开发者_运维知识库 like the first row to have a dynamic height that is based on the size of the height of the Label in my cell. How can I get this height?
{
CGFloat rowHeight = 0;
if(indexPath.section == kBioSection) {
switch(indexPath.row) {
case kBioSectionDescriptionRow:
rowHeight = 100;
break;
case kBioSectionLocationRow:
rowHeight = 44;
break;
case kBioSectionWebsiteRow:
rowHeight = 44;
break;
}
}
else {
rowHeight = 44;
}
return rowHeight;
}
NSString has a method called
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode;
declared in UIStringDrawing.h
It will give you the size required to draw that string.
You can get the height of that required size, and add whatever else you want, like other labels/views, and space between them, to calculate the final height.
Something like this:
{
CGFloat rowHeight = 0;
if(indexPath.section == kBioSection) {
switch(indexPath.row) {
case kBioSectionDescriptionRow:
CGSize labelSize = [descriptionText sizeWithFont:labelFont forWidth:tableView.frame.size.width - 20 lineBreakMode:UILineBreakModeWordWrap]; //Assuming 10 px on each side of the label
rowHeight = labelSize + 50; //Assuming there are 50 px of extra space on the label, besides the text
break;
case kBioSectionLocationRow:
rowHeight = 44;
break;
case kBioSectionWebsiteRow:
rowHeight = 44;
break;
}
}
else {
rowHeight = 44;
}
return rowHeight;
}
Please go through the below link:
http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ -- This tutorial clearly explains you how to increase UITableViewCell height dynamically.
you can use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
}
set height for cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;//set as per your need
}
精彩评论