通过单元格内textView的内容更改单元格高度

时间:2015-09-18 12:56:02

标签: ios iphone xcode swift uitableview

我有一个名为todoTableView的表视图,其中包含由用户创建的单元格。 每个单元格都有文本视图。 我想通过文本视图的内容更改单元格的高度。 这就是我试过的:

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
    }

3 个答案:

答案 0 :(得分:23)

使用边际约束从所有方面绑定textview cell。(前导,尾随,顶部和底部约束

enter image description here

  • 禁用textView滚动

viewDidLoad()中添加以下内容。

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

这将根据您的textview内容大小制作单元格大小。

看看结果:

enter image description here

您不需要编写 heightForRowAtIndexPath

答案 1 :(得分:-1)

Irfan's answer对我不起作用。

我去大小检查员后检查"自动" for" Row Height"。

答案 2 :(得分:-3)

您还应该实施heightForRowAtIndexPath

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