29

I need to make a UITableViewCell that holds alot of text. I know I can add a UITextView to my cell, but each entry will not have the same amount of text.

I know I can control the height of the UITableViewCell with: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, but thats not really what I am looking for.

Scenario 1:

 ---------------
| First Cell    |
 ---------------
 ---------------
| Second Cell   |
| with some     |
| text.         |
 ---------------
 ---------------
| Third Cell    |
 ---------------

Scenario 2:

 ---------------
| First Cell    |
 ---------------
 ---------------
| Second Cell   |
| with some     |
| more text and |
| an unknown    |
| cell height.  | 
 ---------------
 ---------------
| Third Cell    |
 ---------------
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • it is not clear what you are looking for, -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath is the standard way to do it. – Guy Ephraim Feb 03 '11 at 18:26
  • 4
    Yes, but when you do not know the height of the cell in advance, it doesnt make sense. – WrightsCS Feb 03 '11 at 18:28

3 Answers3

34

Use UILabel for your cell text. You can then use sizeWithFont:constrainedToSize: to calculate the height of that UILabel within each cell. For example:

#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;
}
viggio24
  • 12,316
  • 5
  • 41
  • 34
Anh
  • 6,523
  • 7
  • 46
  • 59
  • 2
    As for the UILabel itself, use `sizeWithFont:constrainedToSize:` to pre-calculate the required size and render it with `drawInRect:withFont:` – Anh Feb 03 '11 at 18:29
  • What kind of wrapping does sizeWithFont:constrainedToSize: assume? – Bogatyr Mar 03 '11 at 11:48
  • +1 thanks for this, I have a dynamic label in a table cell and this worked perfectly! I also had to set the frame of the label in cellForRowAtIndexPath to the textSize calculation. – Bogatyr Mar 03 '11 at 17:54
  • 1
    @Bogatyr: It uses `UILineBreakModeWordWrap`. Switch to `sizeWithFont:constrainedToSize:lineBreakMode:` if you want to specify this yourself. – Anh Mar 04 '11 at 10:15
  • Does only UILabel lay out text using this size? Or does it also work with a UITextView? – Bogatyr Mar 04 '11 at 11:43
  • This is really the way to do this? Having the UITableViewViewController needing to know intimate details about the cell breaks encapsulation principles. There has to be a better way. Why should anything other than the UITableViewCell and the UITableView need to know anything about the height of the cell? – meddlingwithfire Jan 11 '12 at 14:59
  • I want to achieve a similar effect; the user to be able to type into each of those cells, so I want to use a UITextView. How is this done? – fatuhoku Jun 26 '14 at 14:54
6

The solution is quite simple and should work since iOS 7. Make sure that the Scrolling Enabled option is turned off for the UITextView inside the UITableViewCell in the StoryBoard.

Then in your UITableViewController's viewDidLoad() set the tableView.rowHeight = UITableViewAutomaticDimension and tableView.estimatedRowHeight > 0 such as:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44.0
}

That's it. UITableViewCell's height will be automatically adjusted based on the inner UITextView's height.

petrsyn
  • 5,054
  • 3
  • 45
  • 48
2

It's rather complicated stuff, as it involves things like the contentInset of he text view you will have to take into account when calculating the texts size. I've written up my learnings and solution for calculating the UITableViewCell height based on an inner UITextView on my blog. The post contains the code that works for universal apps, both table view styles and autorotation.

d11n
  • 997
  • 1
  • 8
  • 7