Automatically change cell height based on content - Swift

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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     } 

回答1:

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


回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!