8

I have a Table View called todoTableView with cells that created by the user. Each cell has Text View. I want to change the height of the cell by the content of the Text View. This is what I tried:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
        cell.bounds.size.height = cell.textView.bounds.size.height

        return cell
    }
Eliko
  • 1,137
  • 4
  • 16
  • 26

3 Answers3

26

Bound Your textview with cell from all sides using marginal constraints.(Leading, Trailing, Top and Bottom constraints)

enter image description here

  • Disable textView Scrolling

In viewDidLoad() add the following.

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

This will make your cell size according to your textview content size.

Have a look at result :

enter image description here

You don't need to write heightForRowAtIndexPath.

Irfan
  • 5,070
  • 1
  • 28
  • 32
  • For some reason it doesn't work, maybe I'm not using marginal as right. Can you tell me please how to do it specific? – Eliko Sep 18 '15 at 17:07
  • 2
    Do you have the same case as above? If yes then select textview and set its leading, trailing, top and bottom constraints to zero with contentView of cell. Make sure you disable scrolling of textview. Try this and let me know if it help. thanks – Irfan Sep 19 '15 at 06:12
  • Ok I have just done it and it still doesn't work.. the keeps writing but the cell height doesn't change – Eliko Sep 19 '15 at 08:48
  • This feature is called self sizing table view. follow step by step one of the two links. http://www.appcoda.com/self-sizing-cells/ http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift. Both are in swift. – Irfan Sep 19 '15 at 09:41
  • Update: When I said It doesn't work - my array had nothing in it, and I added content to it in the App while its running, In this case the cell height wasn't updated. But now I put content in the array before running the app and it changes! but still do not change while I change the content while running the app. – Eliko Sep 19 '15 at 12:37
  • The issues you mention have nothing to do with self sizing cells. You should post a separate post about these. – Earl Grey Sep 23 '15 at 08:28
  • This answer is awesome!. tableView.estimatedRowHeight is necessary but increasing and decreasing it doesn't seem to affect the cell hight? – fullmoon May 23 '16 at 05:55
  • @fullMoon Its for the performance, we provide the average estimated height to minimize calculation, tableview will directly create cell with estimated height and then increase or decrease cell height according to content – Irfan Sep 28 '16 at 13:11
-2

Irfan's answer didn't work for me.

It works after I go to Size Inspector and check "Automatic" for "Row Height".

aaron
  • 39,695
  • 6
  • 46
  • 102
-3

You should also implement heightForRowAtIndexPath:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
    return // Calculate your custom row height here.
}
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • What do I write after the return? I want it to be dynamic and not static. – Eliko Sep 18 '15 at 13:06
  • You'll need to calculate the row height and this depends very much on exactly what you want to do. You can get the height based on the cell by retrieving it, cache it upon cell creation, or determine it by calculating the text height (by temporarily adding a view or through `sizeThatFits`. See http://stackoverflow.com/questions/19049532/how-do-i-size-a-uitextview-to-its-content-on-ios-7 for more methods. – Peter DeWeese Sep 18 '15 at 13:11
  • This solution is outdated and is computationaly more expensive than the canonical one. iOS 8 allows automatic cell sizing as suggested by @Irfan. – Earl Grey Sep 18 '15 at 13:33
  • If you don't need iOS 7 then that is absolutely right. I don't see a version mentioned. – Peter DeWeese Sep 18 '15 at 13:42