UITextView and cell dynamically update height based on content of textview?

纵然是瞬间 提交于 2019-12-18 05:22:28

问题


I have been having an issue regarding a UITextView inside a custom cell. The textView works perfectly besides my issue. I have the UITextViewDelegate methods setup so when something happens to the textview, I have those methods connected.

I would like to know how I can have my custom cell dynamically increase it's height as the user types. Once the user hits the end of the line of the textview, to also have the textview add another line automatically. I have been searching online for a while, and most of the examples for this issue are in objective c. Not much swift code. I would appreciate any help.

I have my cell hooked up in the tableview file as such..

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

      let cell: cellCustom =  the_TableView.dequeueReusableCellWithIdentifier("cell") as! cellCustom

        return cell
 }

In the custom cell file, I have the delegates set up...

func textViewDidChange(textView: UITextView) {


    }


func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {


        return true
    }

I have tried some things like ...

self.textview.sizeToFit() 

self.the_TableView.rowHeight = UITableViewAutomaticDimension
        self.the_TableView.estimatedRowHeight = 47

When I did that, the textview took all the text, but did not increase in size until I resigned it's first responder and scrolled up for the cell to be gone. Then when the cell bounces back down, it updates.

I have already added all the constraints and the project has zero risks or errors and works perfectly.

To clarify, I would like the uitextview to change height based on the content the user types while they are typing it, meaning the custom cell and tableview would have to update. Think of the clear app on iPhone. It's like that. The cell updates dynamically as the user types, increasing in height, while adding a new line once the user reaches the end. That is what I'm reaching for.

Though I am not new to developing by any means, I never really got to this point in development before. Any help would be extremely appreciated. Since I have my custom cell in another file, I have difficulty accessing my cell elements from the tableview file. Still a novice I guess. Thanks for reading this.


回答1:


This works for ios 7 too

this goes into your tableviewcell

protocol HeightForTextView {
    func heightOfTextView(height: CGFloat)

}

class TableViewCell: UITableViewCell {

    @IBOutlet weak var textView: UITextView!

    var delgate:HeightForTextView?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func textViewDidChange(textView: UITextView) {

        var fixedWidth: CGFloat = textView.frame.size.width
        var newSize: CGSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat.max))

        if let iuDelegate = self.delgate {

            iuDelegate.heightOfTextView(newSize.height)
        }


    }

}

your tableview controller should be

 class TableViewController: UITableViewController,HeightForTextView {

        var textViewHeight = CGFloat()

    //In your cell for row method set your your delegate 
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    cell.delgate = self

    return cell
}


//delegate method

    func heightOfTextView(height: CGFloat) {

    textViewHeight = height
    self.tableView.beginUpdates()
    self.tableView.endUpdates()

    }

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

            return textViewHeight + 70
    }

    }


来源:https://stackoverflow.com/questions/31485558/uitextview-and-cell-dynamically-update-height-based-on-content-of-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!