Change cell height by the content of the textView inside the cell

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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     } 

回答1:

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

  • 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 :

You don't need to write heightForRowAtIndexPath.



回答2:

Irfan's answer didn't work for me.

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



回答3:

You should also implement heightForRowAtIndexPath:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat  {     return // Calculate your custom row height here. } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!