动态调整IOS tableview单元格的大小,其中textview嵌入了swift

时间:2017-10-31 10:51:42

标签: ios swift uitableview textview row-height

我尝试了以下代码,它提供了正确的textview框架高度

func textViewDidChange(_ textView: UITextView) {

    myToDoList[keyArray[sect]]![row] = textView.text
    var frame = textView.frame
    frame.size.height = textView.contentSize.height
    textView.frame = frame
    print(frame)

    let indexPath = self.tableView.indexPathForView(textView)
    print(inputActive,indexPath)

    self.tableView.indexPathForView(inputActive)
    self.tableView.rowHeight = frame.size.height
}

3 个答案:

答案 0 :(得分:3)

" Krunal"缺少一两件......

从单元格布局/约束开始:

enter image description here

并使用此代码:

import UIKit

class WithTextViewCell: UITableViewCell, UITextViewDelegate {

    @IBOutlet var theTextView: UITextView!

    var callBack: ((UITextView) -> ())?

    override func awakeFromNib() {
        super.awakeFromNib()
        // in case these were not set in IB
        theTextView.delegate = self
        theTextView.isScrollEnabled = false
    }

    func textViewDidChange(_ textView: UITextView) {
        // tell controller the text changed
        callBack?(textView)
    }

}

class TableWithTextViewTableViewController: UITableViewController {

    var cellData = [
        "UITableViewController implements the following behaviors:",
        "If a nib file is specified via the init(nibName:bundle:) method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.",
        "If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.",
        "When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear(_:). You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.",
        "When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear(_:).",
        "It implements the superclass method setEditing(_:animated:) so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.",
        "You create a custom subclass of UITableViewController for each table view that you want to manage. When you initialize the controller in init(style:), you must specify the style of the table view (plain or grouped) that the controller is to manage. Because the initially created table view is without table dimensions (that is, number of sections and number of rows per section) or content, the table view’s data source and delegate—that is, the UITableViewController object itself—must provide the table dimensions, the cell content, and any desired configurations (as usual). You may override loadView() or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.",
        ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100

    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WithTextViewCell", for: indexPath) as! WithTextViewCell

        // Configure the cell...
        cell.theTextView.text = cellData[indexPath.row]

        cell.callBack = {
            textView in
            // update data source
            self.cellData[indexPath.row] = textView.text
            // tell table view we're starting layout updates
            tableView.beginUpdates()
            // get current content offset
            var scOffset = tableView.contentOffset
            // get current text view height
            let tvHeight = textView.frame.size.height
            // telll text view to size itself
            textView.sizeToFit()
            // get the difference between previous height and new height (if word-wrap or newline change)
            let yDiff = textView.frame.size.height - tvHeight
            // adjust content offset
            scOffset.y += yDiff
            // update table content offset so edit caret is not covered by keyboard
            tableView.contentOffset = scOffset
            // tell table view to apply layout updates
            tableView.endUpdates()
        }

        return cell
    }

}

" key"部分:

  1. 添加"回拨"封闭到你的单元格,所以我们可以在文本发生变化时告诉控制器。

  2. 发生回调时,请使用表视图控制器:使用编辑后的文本更新dataSource;告诉文本视图调整自身的大小;并调整内容偏移量以避免插入符号(文本插入点)消失在键盘后面。

答案 1 :(得分:0)

当您将interfaceBuilder直接设计为原型单元格或xib时,很容易实现动态单元格高度,您只需要正确设置底部约束,其余部分由tableViewAutoDimension完成。

override func viewDidLoad() {
  super.viewDidLoad()
  tableView.estimatedRowHeight = 50
  tableView.rowHeight = UITableViewAutomaticDimension
}

Desingning cell for auto dimension

答案 2 :(得分:0)

使用UITextview根据您的内容(文字)大小设置sizeToFit高度,并在translatesAutoresizingMaskIntoConstraints中启用cellForRowAtIndexPath

试试看

class TextViewCell: UITableViewCell {

    @IBOutlet weak var textview: UITextView!


    func adjustTextViewHeight(textview : UITextView) {
        textview.translatesAutoresizingMaskIntoConstraints = true
        textview.sizeToFit()
        textview.isScrollEnabled = false
    }
}


class TableController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var table: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Don't forget to set dataSource and delegate for table
        table.dataSource = self
        table.delegate = self

        // Set automatic dimensions for row height
        table.rowHeight = UITableViewAutomaticDimension
        table.estimatedRowHeight = UITableViewAutomaticDimension
    }



    // UITableViewAutomaticDimension calculates height of label contents/text
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "textview") as! TextViewCell
        cell.adjustTextViewHeight(textview: cell.textview)
        return cell
    }

}

这是故事板布局:

enter image description here

结果如下:

enter image description here