1

I'm currently in the process of building a social networking app that displays the user's posts in a table. Each post will be displayed in one cell of the table. Because I don't know how much the user will type for one post, I need to set the text view size according to the amount of text typed by the user. The problem is that my current implementation is not working and behaving strangely.

Here is my table code:

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

    let cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HomeCell") as? UITableViewCell
    let post : Post = tblData[indexPath.row]

    //Get all custom cell components
    let userLbl = cell?.viewWithTag(101) as UILabel
    let timeLbl = cell?.viewWithTag(102) as UILabel
    let postLbl = cell?.viewWithTag(103) as UITextView
    let profilePic = cell?.viewWithTag(100) as UIImageView
    let postPic = cell?.viewWithTag(104) as UIImageView

    //Post lbl properties
    let textHeight = textViewDidChange(postLbl)
    postLbl.frame.size.height = CGFloat(textHeight)
    //postLbl.selectable = false

    //Individual height variables
    let postInfoHeight = 66 as CGFloat
    var postHeight = 8 + CGFloat(textHeight)
    var imgHeight = 8 + postPic.frame.height as CGFloat

    if post.postInformation == "" {
        postLbl.removeFromSuperview()
        postHeight = 0
    }

    if post.img == nil {
        postPic.removeFromSuperview()
        imgHeight = 0
    }

    //Change the autolayout constraints so it works properly
    return postInfoHeight + postHeight + imgHeight
}

and here is the code that calculates the height of the text view:

func textViewDidChange(textView : UITextView) -> Float {

    let content : NSString = textView.text
    let oneLineSize = content.sizeWithAttributes(["NSFontSizeAttribute": UIFont.systemFontOfSize(14.0)])

    let contentSize = CGSizeMake(textView.frame.width, oneLineSize.height * round(oneLineSize.width/textView.frame.width))

    return Float(contentSize.height)
}

I can't understand why this isn't giving me the correct results. If anyone can spot an error or suggest how to solve this issue I'd really appreciate it. Thanks in advance!

user1615326
  • 97
  • 1
  • 1
  • 7
  • What results is it giving you? Too tall or too short? Cutting off lines? – jrisberg Jun 25 '15 at 21:06
  • Almost every text view is being set to a constant height. A few of them are slightly taller or slightly shorter. It's really strange, the height of the text view seems to not have anything to do with the text. I can't tell if it's a problem in textViewDidChange method or the table methods – user1615326 Jun 25 '15 at 21:09
  • You've checked that `postLbl` isn't nil and has text in it? – jrisberg Jun 25 '15 at 21:12
  • 1
    Possible duplicate of http://stackoverflow.com/questions/4890054/uitableviewcell-uitextview-with-dynamic-height : check out the answers there and see if it helps – jrisberg Jun 25 '15 at 21:13

1 Answers1

0

Thanks to @jrisberg for the link to the other question. I decided to abandon using the text view and am now using a UILabel instead. The code in the other question is extremely old and uses a lot of deprecated methods, so I'll post some updated code that works on iOS 8 here. I deleted the textViewDidChange(textView : UITextView) method and changed my tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) method to this:

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

    let cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HomeCell") as? UITableViewCell
    let post : Post = tblData[indexPath.row]

    //Get custom cell components
    let postLbl = cell?.viewWithTag(103) as UILabel
    let postPic = cell?.viewWithTag(104) as UIImageView

    //Post lbl properties
    let PADDING : Float = 8
    let pString = post.postInformation as NSString?

    let textRect = pString?.boundingRectWithSize(CGSizeMake(CGFloat(self.tableView.frame.size.width - CGFloat(PADDING * 3.0)), CGFloat(1000)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(14.0)], context: nil)

    //Individual height variables
    let postInfoHeight = 66 as CGFloat
    var postHeight = textRect?.size.height
    postHeight? += CGFloat(PADDING * 3)
    var imgHeight = 8 + postPic.frame.height as CGFloat

    if post.img == nil {
        postPic.removeFromSuperview()
        imgHeight = 0
    }

    //Change the autolayout constraints so it works properly
    return postInfoHeight + postHeight! + imgHeight
}
user1615326
  • 97
  • 1
  • 1
  • 7