I have an application in which I am loading a UITableView
from 3 types custom cells
. I made 3 custom classes for that and added all the elements Progra
ill add another example of measuring a more complex cell. mind my measurement code isnt 100% correct but it shows the approach
+ (CGFloat)calculatedCellHeightForTweet:(id)tweet width:(CGFloat)width {
//text
CGSize s1 = [tweet[@"text"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(width, MAXFLOAT)
lineBreakMode:NSLineBreakByWordWrapping];
//user
NSString *text;
if(![tweet[@"to_user"] isKindOfClass:[NSNull class]])
text = [NSString stringWithFormat:@"%@ > %@", tweet[@"from_user"], tweet[@"to_user"]];
else
text = tweet[@"from_user"];
CGSize s2 = [text sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont labelFontSize]]
forWidth:width
lineBreakMode:NSLineBreakByWordWrapping];
return fmaxf(s1.height + s2.height + /*padding*/ 44, 60);
}
Please check the custom cell height will be the same as table view each cell height.
You can use following function to adjust table view's cell height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return height;
}
You can use code like this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Defination *definition = [self.dataSource objectAtIndex:indexPath.row];
NSString *detailString = definition.detailString;
CGSize detailSize;
detailSize = [detailString sizeWithFont:fontSize(12.0) constrainedToSize:CGSizeMake(420, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height + 20.0;
}
you can calculate height by using below function
-(CGSize)SizeOfString:(NSString *)string withFont:(UIFont *)font constrainedToWidth:(CGFloat)width
{
CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, 4000) lineBreakMode:UILineBreakModeWordWrap];
return CGSizeMake(width, size.height + 10);
}
Call
CGSize size = [self SizeOfString:your_text withFont:your_font constrainedToWidth:width_of_showing_area];