【问题标题】:How to update height of the cell with UITableViewAutomaticDimension?如何使用 UITableViewAutomaticDimension 更新单元格的高度?
【发布时间】:2015-12-06 11:11:16
【问题描述】:

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

如果用户在UITextView添加新行,所以textView,单元格高度应该增加,我该如何更新高度?

试过了:

cell.layoutIfNeeded()
cell.setNeedsUpdateConstraints()

这些不起作用。

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

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

这行得通,但是我需要处理辞职并显示键盘。我会避免它。有什么解决方法吗?

【问题讨论】:

    标签: ios uitableview uitextview


    【解决方案1】:

    需要添加如下方法:

    func textViewDidChange(textView: UITextView) {
    
        tableView.beginUpdates()
        tableView.endUpdates()
    }
    

    它会在不重新加载 tableview 的情况下更新约束

    【讨论】:

    • 此答案无需重新加载表视图即可工作。在 iOS9 中测试。
    • 这在改变 view 而不是 textView 的高度约束时也有效。在 iOS10 中测试。
    【解决方案2】:

    一段时间后textView 将开始滚动以使其在新行上增加高度,每当您需要实现textViewDidChangeText: 方法并通过计算文本高度来增加其高度时。

    要使用UITableViewAutomaticDimension,您需要从上到下为subviews添加约束,我认为您还应该为textView的高度约束创建IBOutlet并在textViewDidChangeText方法上设置常量值,在这种情况下@987654326 @height 会动态变化。

    【讨论】:

    • 如果我设置了高度限制,它将停止 UITableViewAutomaticDimension 工作
    • 然后通过设置框架来增加它的高度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多