Custom Dynamic Cell Height UITableView

I have a search and search on endless blogs and articles on how to determine the dynamic height for a custom UITableViewCell and its long text. It was really hard for me to find good documentation about this.

I need to make the cell grow in accordance with the text inside, but never fall below a height of 70.

I tried several answers to this question here on StackOverflow, but none of them worked. My application is completely finished, but I really need to achieve this before I release it, and its troublesome.

This is what I am trying, but I just get a detachment of cells overlapping each other. From what I read, I need to find the frame if the custom cell or text view is in the cell, but I'm not sure how to do this or mix everything together to return the same height.

Any help would be greatly appreciated Thank you!

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { CGSize aSize; aSize = [[(Tweet*)[tweets objectAtIndex:indexPath.row]tweet] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeTailTruncation]; return aSize.height; } 
+7
source share
6 answers

I had a similar problem and it helped me a lot.

 #define PADDING 10.0f - (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *text = [self.items objectAtIndex:indexPath.row]; CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)]; return textSize.height + PADDING * 3; } 
+18
source

Hey, so you will need to store the list of strings in NSArray, and then you will need to calculate the height of nsstring using sizeWithFont: constrainedToSize: the documentation is here http://developer.apple.com/library/ios/#documentation/UIKit/Reference /NSString_UIKit_Additions/Reference/Reference.html

so your tableView method should look something like this:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:"Helvetica" size:9] forKey: NSFontAttributeName]; CGSize cell_size = [string boundingRectWithSize:CGSizeMake(300,999) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size; if (cell_size.height > 70) { return cell_size.height; } else { return 70; } } 

EDIT: THIS IS UPDATED FOR iOS 7

+3
source

I just wrote about this problem and how I finally decided to solve it. You can read about it here: Dynamic cell height of a UITableView based on content

Basically, I created a subclass of UITableView that automates the processing and calculation of dynamic cell heights for standard and custom cells. This is not a silver bullet and probably needs to be expanded and customized, but I used it as in several applications with good results.

You can get the code here: https://github.com/danielsaidi/AutoSizeTableView

Hope this helps!

(... and if this did not happen, I would love to hear why not)

0
source

I tried many solutions, but the one that worked was this suggested by a friend:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int height = [StringUtils findHeightForText:yourLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:17.0f]]; height += [StringUtils findHeightForText:yourOtherLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:14.0f]]; return height + CELL_SIZE_WITHOUT_LABELS; //important to know the size of your custom cell without the height of the variable labels } 

Class StringUtils.h:

  #import <Foundation/Foundation.h> @interface StringUtils : NSObject + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font; @end 

Class StringUtils.m:

  #import "StringUtils.h" @implementation StringUtils + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font { CGFloat result = font.pointSize+4; if (text) { CGSize size; CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil]; size = CGSizeMake(frame.size.width, frame.size.height+1); result = MAX(size.height, result); //At least one row } return result; } @end 

It worked great for me. I had a special cell with 3 images with fixed sizes, 2 labels with fixed sizes and 2 variable labels. Worked like a charm. Hope this works for you too.

Best regards, Alexander.

0
source

iOS 7

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { NSString *text = @"Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello HelloHello HelloHello HelloHello HelloHello HelloHello Hello"; CGRect rect = [text boundingRectWithSize:CGSizeMake(280.0f, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont fontWithName:@"Hellvetica" size:14.0f] } context:nil]; return CGSizeMake(320.0f, ceil(rect.size.height) + 10.0f); } 
0
source

Use the UITableViewDelegate method:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // here you need to get height of your textlabel in your custom cell. MyCustomCell *myCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath]; return myCell.myTextLabel.frame.size.height + 20; } 

Something like that

-4
source

All Articles