Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

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.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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!
    
    weak 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
    }
    
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...