tableview cell how do we resize cell in swift along with image and label

为君一笑 提交于 2019-11-26 22:18:45

问题


when we have a lable and image in tableview cell how do we resize cell in swift or lable as per the text in the Last cell(in picture) there are some more lines of text but text is cutting off how we resolve it


回答1:


Give constrain to your lable hight greater equal and put line to 0.

var pickheight: CGFloat = 0.0

Write this line in

override func viewDidLoad() {
        super.viewDidLoad()
        tableTrip.rowHeight = UITableViewAutomaticDimension
}

Method for increase tableview cell .

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

     pickheight = self.findHeightForText("Pass your text here like array value as String ", havingWidth: self.view.frame.size.width - 116, andFont: UIFont.systemFontOfSize(14.0)).height

     return "YOUR_DEFALUT_CELL_SIZE" + pickheight
}

Method for find text hight for cell..

func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {
        var size = CGSizeZero
        if text.isEmpty == false {
            let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
            size = CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
}



回答2:


  1. set top,bottom,leading,trailing constraints for your label
  2. set Attribute Inspector->lines to 0
  3. set tableview.rowHeight = UITableViewAutomaticDimension in viewDidLoad() method



回答3:


All you really have to do is:

  1. Use Auto Layout when creating your table view cells.
  2. Set the table view rowHeight to UITableViewAutomaticDimension.
  3. Set the estimatedRowHeight or implement the height estimation delegate method.

    tableView.rowHeight = UITableViewAutomaticDimension

    tableView.estimatedRowHeight = 200 // Set maximum height needed for the table row

The trick to getting Auto Layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints. Then, the intrinsic height of the subviews will be used to dictate the height of each cell.

Please, Read this article for details and working sample: https://www.raywenderlich.com/129059/self-sizing-table-view-cells



来源:https://stackoverflow.com/questions/39888512/tableview-cell-how-do-we-resize-cell-in-swift-along-with-image-and-label

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