how to set table cell height dynamically depending on text label length?

I am trying to learn Obj-C and iOS programming, but am still very new. I decided to try and make a simple Reddit client application. I am trying to display the front page posts within a UITableView, with each post represented by its own cell.

On the cell, here is how I am setting the title:

cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [[tempDictionary objectForKey:@"data"] objectForKey:@"title"];   
[cell.textLabel sizeToFit];

However, the cell ends up clipping itself if the title text is too long. Here's a picture:

How can I make my cells automatically adjust their heights to accommodate longer title labels, without intersecting other cells or the detail text label?

Thank you for any help!


You should check out the documentation reference for

-[UITableViewDelegate tableView:heightForRowAtIndexPath:]

Essentially, you will need to measure the text yourself in order to return the correct dimensions for each table row. If they're simple NSStrings, which they appear to be from your screenshot, you can use the NSString UIKit Additions to measure them, e.g.

-[NSString sizeWithFont:constrainedToSize:]

Where the constrained size could have infinite height but the width of your table cell's content view.

If they are attributed strings, there is a similar method in the NSAttributedString UIKit additions called boundingRectWithSize:options:context:

For new iOS 8 there is a new way to make this simple.

There is new a parameter that calculates your cell height with respect of your auto layout constraints. This is UITableViewAutomaticDimension. You can use it in the viewWillAppear method of your view controller.

Objective C:

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.tableView.estimatedRowHeight = 70.0; // for example. Set your average height 
  self.tableView.rowHeight = UITableViewAutomaticDimension;
  [self.tableView reloadData];


override func viewWillAppear(animated: Bool) {
    self.tableView.estimatedRowHeight = 70 // for example. Set your average height 
    self.tableView.rowHeight = UITableViewAutomaticDimension


Work nice and as you want at your example. As for me, I add height things in viewDidLoad and left in viewWillAppear only reloadData(). There is also useful source.

From documentation: The default value of rowHeight is UITableViewAutomaticDimension. I leave the code as it is, for now, but keep in mind that you don't need to set row height in iOS 8+.

Need Your Help

Useraccounts updated to Useraccounts:iron-routing

meteor iron-router meteor-accounts

updated my meteor app and had useraccounts on it and it suddenly told me to install the useraccounts:iron-routing to use it with Iron Router...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.