iOS 8 auto-sizing UITableViewCell with UITextView

前端 未结 6 1562
余生分开走
余生分开走 2021-02-12 12:11

iOS 8 introduced a way for tableViews to automatically adjust their cell\'s height based on their content (via AutoLayout).

// in viewDidLoad:
tableView.rowHeigh         


        
6条回答
  •  执笔经年
    2021-02-12 13:01

    The tableView has to be informed that the textView has changed. Other posts usually answer a "static" textView problem. However, if you are typing inside a table, the cell needs to grow as the textView grows as you type. The textView grows by disabling scrolling. The cell grows by setting the textView's top and bottom constraints to those of the contentView of the cell. This will work once, once the table loads. However to tell the tableView to grow in real time, you have to do in the textView's didChange delegate call

    func textViewChanged(onCell cell: YourCustomCell) {
        UIView.setAnimationsEnabled(false)
        tableView.beginUpdates()
        tableView.endUpdates()
        UIView.setAnimationsEnabled(true)
    }
    

    This will work as you expect. The cell will grow as you type and there won't be a "bouncing".

提交回复
热议问题