Daniel Bramhall
Daniel Bramhall

Reputation: 1501

Resize a specific table view cell depending on contentSize of UITextView

I am trying to resize a specific UITableViewCell depending on the size of a UITextView - I have been trying for quite a while and seem to have absolutely no luck.

What I have done is calculate the size of the UITextView depending on its contentSize (which varies) and then returning the height within a CGFloat and then passing that the row height method and manipulating the height of it from there.

let contentSize = self.myTextView.sizeThatFits(self.myTextView.bounds.size)
var frame = self.myTextView.frame
frame.size.height = contentSize.height
self.myTextView.frame = frame

rowHeight = frame.height

I then have a CGFloat declared called rowHeight and then attempt to pass contentSize.height into it which seems to work but then the it doesn't change the size of the cell.

Here's the method:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    if (indexPath.section == 2 && indexPath.row == 0) {
        return rowHeight
    }
    else {
        return 44.0
    }
}

I have printed frame.height.description as a string in the console and then specifically defined it in the row height method and this works fine. This leads me to believe that my issue is calculating and placing the CGFloat within the rowHeight variable.

Can someone please guide me how to pass a CGFloat based on the size of a UITextView into a variable and then using that variable as the row height of a specific cell?

Upvotes: 0

Views: 142

Answers (1)

trusk
trusk

Reputation: 1703

From apple's docs

func reloadData()

Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on.

So this should redraw your table with the new height.

Upvotes: 1

Related Questions