如何使用UITableViewAutomaticDimension更新单元格的高度? [英] How to update height of the cell with UITableViewAutomaticDimension?

查看:320
本文介绍了如何使用UITableViewAutomaticDimension更新单元格的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableViewCell中有一个 UITextView ,并使用iOS 8提供的 UITableViewAutomaticDimension 方法here

I have a UITextView in a tableViewCell, and using the iOS 8 provided UITableViewAutomaticDimension approach listed here.

如果用户将新行添加到 UITextView ,那么textView,单元格高度应该增加,我该如何更新高度?

If user add new line to UITextView, and so textView, cell height should increase, how can I update height?

尝试:

cell.layoutIfNeeded()
cell.setNeedsUpdateConstraints()

这些不起作用。

如果我重新加载单元格,比如

If I reload cell, like

tableView.reloadRowsAtIndexPaths([iindexPath], withRowAnimation: .None)

这是有效的,但后来我需要处理辞职和显示键盘。我会避免它。任何解决方法?

this works, but then I need to deal with resign and show keyboard. I would avoid it. Any workaround?

推荐答案

需要添加以下方法:

func textViewDidChange(textView: UITextView) {

    tableView.beginUpdates()
    tableView.endUpdates()
}

它将更新约束而不重新加载tableview

it will update constraints without reloading tableview

这篇关于如何使用UITableViewAutomaticDimension更新单元格的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆