Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically change cell height based on content - Swift

Using a UITableView in Swift, could someone please help me automatically change the height of the cell based on the label, picture, and description please?

All of the information passes through correctly, I just need help formatting it.

I tried adjusting it using cell.frame.size.height, but that had no effect. I could change the size of the cell in the storyboard, but I would like it more dynamic if possible.

Thank you in advance!

My cell is as follows:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

        // Creator
        let creator = self.photos[indexPath.row].creator
        let user = UILabel()
        user.frame = CGRectMake(self.headerView.frame.origin.x, self.headerView.frame.origin.y + self.headerView.frame.height + 3, self.view.frame.width, 12)
        user.text = creator
        cell.addSubview(user)

        // Photo
        let picture = self.photos[indexPath.row].image()
        let imageView = UIImageView(image: picture!)
        imageView.frame = CGRectMake(user.frame.origin.x, user.frame.origin.y + user.frame.height, self.view.frame.width, 400)
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        cell.addSubview(imageView)

        // Photo description
        let description = UITextView()
        let des = self.photos[indexPath.row].photoDescription
        description.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.height, self.view.frame.width, 65)
        if des != nil {
            description.text = des!
            description.font = UIFont.systemFontOfSize(16)
            description.editable = false
            description.scrollEnabled = false
        }
        cell.addSubview(description)

        cell.frame.size.height = user.frame.size.height + imageView.frame.size.height + description.frame.size.height + 30

        return cell
    }
like image 467
jape Avatar asked Dec 04 '22 03:12

jape


2 Answers

In order to make the cell auto sizeable, you need to do two things:

  1. Use autolayout to arrange the elements inside of your cell. Bottom space and top space constraints will push the cell size as needed.
  2. Set tableView.rowHeight = UITableViewAutomaticDimension in your viewDidLoad
like image 195
MirekE Avatar answered Dec 22 '22 06:12

MirekE


If your cell height is dependent on text size then follow this: Add the method in your ViewController.swift

func calculateHeight(inString:String) -> CGFloat {
     let messageString = inString
     let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 15.0)]

     let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)

     let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 222.0, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)

      let requredSize:CGRect = rect
      return requredSize.height
}

Set the width of your text label Then call this function:

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            heightOfRow = self.calculateHeight(inString: conversations[indexPath.row].description)

            return (heightOfRow + 60.0)
    }

For Basic cell:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return UITableViewAutomaticDimension
    }

This function will not work for custom cells.

If your cell height is dependent on image height, then return image height + someFloatValueAccordingToYourChoice.

Hope it will work.

like image 35
Rubaiyat Jahan Mumu Avatar answered Dec 22 '22 05:12

Rubaiyat Jahan Mumu