3

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.

RandomDude
  • 37
  • 1
  • 9
  • Chain your constraints, textView to top of cell, textView to bottom of cell, and set your textview's height constraint as fixed height.And add another constraints you needed.Then right after you changed the textView's height constraint, you call `layoutIfNeeded()` for the current cell – Okhan Okbay Jul 17 '15 at 22:35
  • @pandarencodemaster Thank you for the input. I have already implemented all my constraints. Adding the height constraint though, throws multiple risks around in the debugger. The constraints work perfectly throughout all devices. Also, could you elaborate regarding the `layoutIfNeeded` ? – RandomDude Jul 17 '15 at 22:44
  • If you want your updates on constraints to work, you should use layoutIfNeeded to make them take effect right after you change any constraint. – Okhan Okbay Jul 17 '15 at 22:48
  • @pandarencodemaster That doesn't seem to work. I have added it toward the cell, but the textview just continues to take the data and doesn't increase in height. The textView is linked to the cell as well. Thanks for trying though. I appreciate it. – RandomDude Jul 17 '15 at 22:56
  • Did you try `self.tableView.layoutIfNeeded()` right after you changed your text view's height? – Okhan Okbay Jul 17 '15 at 23:06
  • Also check this [link](https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/) – Okhan Okbay Jul 17 '15 at 23:10
  • @pandarencodemaster I never set the height programatically. I just added the constraints of the uitextview from the top, bottom, and left to the cell, as well as other relating elements in the cell. The textview height in the IB is 30. Also, thank you so much for helping me. I really appreciate it. Thanks for the link. – RandomDude Jul 17 '15 at 23:12

1 Answers1

10

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
    }
    
    }
Aafaq
  • 202
  • 1
  • 13
  • Thank you! This works. I had to have an extension though in order to call the tableview since the textview is in a separate file. It's technically making a new tableview. A bit sloppy. – RandomDude Jul 18 '15 at 02:37
  • Write a delegate or add an observer in that file,so that you can get the height and reload the tableview when text changes.If its not clear i can help you on this... – Aditya Koukuntla Jul 18 '15 at 03:23
  • I would appreciate how you would do that. Ever since I added the extension, my button is lagging by 2 seconds which is unacceptable when it comes to UI. I'd really like your help. How would you add an observer? My custom cell is in another file so I can't access my tableview. How would you approach this? Thank you again for your answer above as well. I appreciate it. – RandomDude Jul 18 '15 at 03:37
  • Thank you so much. I appreciate it greatly! – RandomDude Jul 18 '15 at 19:33
  • can i get this in objective c for collection view cell? – Mansi Dobariya Sep 27 '18 at 11:16
  • @AdityaKoukuntla Showing "extensions must not contain stored properties" error to me at line var textViewHeight = CGFloat(). Can You please help me? – Muju Aug 03 '20 at 05:19