根据 UITextView 的 contentSize 调整特定 table 视图单元格的大小

Resize a specific table view cell depending on contentSize of UITextView

我正在尝试根据 UITextView 的大小调整特定 UITableViewCell 的大小 - 我已经尝试了很长时间,但似乎完全没有运气。

我所做的是根据其 contentSize(变化的)计算 UITextView 的大小,然后返回 CGFloat 内的高度,然后传递该行高度方法并从那里操纵它的高度。

let contentSize = self.myTextView.sizeThatFits(self.myTextView.bounds.size)
var frame = self.myTextView.frame
frame.size.height = contentSize.height
self.myTextView.frame = frame

rowHeight = frame.height

然后我声明了一个名为 rowHeight 的 CGFloat,然后尝试将 contentSize.height 传递给它,这似乎有效,但它不会改变单元格的大小。

方法如下:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    if (indexPath.section == 2 && indexPath.row == 0) {
        return rowHeight
    }
    else {
        return 44.0
    }
}

我已经在控制台中将 frame.height.description 作为字符串打印出来,然后在行高方法中专门定义了它,这工作正常。这让我相信我的问题是计算 CGFloat 并将其放置在 rowHeight 变量中。

有人可以指导我如何根据 UITextView 的大小将 CGFloat 传递给变量,然后将该变量用作特定单元格的行高吗?

来自apple's docs

func reloadData()

Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on.

所以这应该用新的高度重新绘制 table。