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

Eli*_*iko 8 iphone xcode uitableview ios swift

我有一个表格视图,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
    }
Run Code Online (Sandbox Code Playgroud)

Irf*_*fan 24

绑定您textviewcell来自四面八方的使用边际约束.(前置,顶部和底部的约束)

在此输入图像描述

  • 禁用textView滚动

viewDidLoad()中添加以下内容.

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

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

看看结果:

在此输入图像描述

您不需要编写heightForRowAtIndexPath.

  • 你有同样的情况吗?如果是,则选择textview并使用cellView的单元格将其前导,尾随,顶部和底部约束设置为零.确保禁用textview的滚动.试试这个,让我知道它是否有帮助.谢谢 (2认同)