Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

ios - Can I change height of UITableViewCell real-time?

I have UITableViewCell and inside of that cell I have a UITextView. When I will change text of UITextView I want to change cell height real time?

How can I do that? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The main idea of this solution is to calculate the textView's height and assigns it to its row.

Note: this is Swift 3 code:

class ViewController: UIViewController {

    // array containing rows heights:
    var rowHeights = [CGFloat]()

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // fill it with default values of row heights
        for _ in 1...10 {
            rowHeights.append(44)
        }
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return rowHeights[indexPath.row]
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // return the actual number of your rows...
        return 10
    }

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

        // assgin the tag of current text view to use for determining the current cell's height
        cell.textView.tag = indexPath.row

        cell.textView.delegate = self

        return cell
    }
}

extension ViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {

        // calculate the current height and append it in the rowHeights depends on the textView's tag. Add your the textView fontSize instead of 15
        rowHeights[textView.tag] = (textView.text?.heightWithConstrainedWidth(width: tableView.frame.size.width, font: UIFont.systemFont(ofSize: 15)))!

        // for updating the tableView appearence (don't use "reloadData", it will resignFirstResponder the textView)
        tableView.beginUpdates()
        tableView.endUpdates()

        textView.setContentOffset(CGPoint.zero, animated: true)

        //textView.becomeFirstResponder()
    }
}

extension String {
    // this method calculates the height of string depending on your view width and font
    func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)

        return boundingBox.height
    }
}

CustomTableViewCell:

class TextViewTableViewCell: UITableViewCell {
    @IBOutlet weak var textView: UITextView!
}

The output should looks like this:

enter image description here

Hope That Helped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...