tableview 单元格我们如何调整 swift 中的单元格以及图像和标签

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

当我们在 tableview 单元格中有一个标签和图像时,我们如何根据最后一个单元格(图片中)中的文本调整 swift 或标签中的单元格大小还有更多行文本但文本切断了我们解决它的方式

将标签的高度限制为大于等于并将行设置为 0。

var pickheight: CGFloat = 0.0

将这一行写在

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

增加tableview单元格的方法.

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
}

查找单元格文本高度的方法..

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
}
  1. 为标签设置顶部、底部、前导、尾部 约束条件
  2. Attribute Inspector->lines 设置为 0
  3. viewDidLoad()方法
  4. 中设置tableview.rowHeight = UITableViewAutomaticDimension

您真正需要做的是:

  1. 创建 table 视图单元格时使用自动布局。
  2. 将 table 视图的行高设置为 UITableViewAutomaticDimension。
  3. 设置 estimatedRowHeight 或实现高度估计委托方法。

    tableView.rowHeight = UITableViewAutomaticDimension

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

让自动布局在 UITableViewCell 上工作的诀窍是确保你有约束将每个子视图固定在所有边上——也就是说,每个子视图应该有前导、顶部,尾随和底部约束。然后,子视图的固有高度将用于指示每个单元格的高度。

请阅读这篇文章以获取详细信息和工作示例:https://www.raywenderlich.com/129059/self-sizing-table-view-cells