I need to be able to adjust the height of a single cell in my UITableView so that it fits the amount of text in its detail label.
I have played with the following but it hasn't work for me:
How do I wrap text in a UITableViewCell without a custom cell
Attempted code:
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:17.0]; }
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellText = @"Go get some text for your cell."; UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0]; CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; return labelSize.height + 20; }
This hasn't worked, it shows the entire string on the cell, however the cell height isn't affected at all.
Its simple, just add this to your code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
It automatically count an height of row and than return a float... :-)
Hope this helps!
Hi Josh,
Using tableView:heightForRowAtIndexPath:
you can give the size of each row at run time. now your problem is how to get height from your string there are function in NSString class by this code your problem,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str = [dataSourceArray objectAtIndex:indexPath.row]; CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping]; NSLog(@"%f",size.height); return size.height + 10; }
by below line you set your label`s num. of line to max. so set it in cellForRowAtIndexPath: method.
cell.textLabel.numberOfLines = 0;
if you use some custom cell then manage all label`s string with this and get sum of all that height then set the height of your cell.
Edit : iOS 8 onwards if you set proper autolayout constraints to label then you have to set only following delegate method to achieve this.
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { //minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension; return UITableViewAutomaticDimension; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
that`s it. no any calculation required. For more information check this tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With