滚动后调整单元格大小(自动布局)

时间:2019-05-27 05:16:48

标签: swift autolayout

在滚动后调整单元格大小。

enter image description here

CommentViewController.swift代码

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    tblView.register(UINib(nibName: "CellComment", bundle: nil), forCellReuseIdentifier: "CellComment")

    // Remove extra space from group tableview
    var frame = CGRect.zero
    frame.size.height = .leastNormalMagnitude
    tblView.tableFooterView = UIView(frame: frame)

    tblView.rowHeight = UITableView.automaticDimension
    tblView.estimatedRowHeight = 123

    tblView.estimatedSectionFooterHeight = 0.01
    tblView.separatorColor = .clear
    tblView.reloadData()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

}

// UITableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 15
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tblView.dequeueReusableCell(withIdentifier: "CellComment", for: indexPath) as! CellComment
    cell.selectionStyle = .none
    cell.delegate = self
    cell.imgViewUser.image = UIImage.init(named: "userImage1.jpg")
    cell.lblUserName.text = "User Name \(indexPath.row + 1)"

    if indexPath.row % 2 == 0 {
        cell.lblComment.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    }
    else {
        cell.lblComment.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."

    }
    //        cell.updateConstraintsIfNeeded() //I try but not working
    //        cell.layoutIfNeeded()
    cell.layoutSubviews()
    return cell
}

CellComment.swift

override func layoutSubviews() {
        super.layoutSubviews()
        //lblComment.preferredMaxLayoutWidth = lblComment.bounds.width
        lblComment.preferredMaxLayoutWidth = lblComment.bounds.size.width
    }

Full Video

2 个答案:

答案 0 :(得分:0)

这是因为tableview无法正确计算单元格的帧。您不需要调用cell.layoutSubviews()。您应该检查对tableview单元的contentView的约束。

答案 1 :(得分:0)

删除此cell.layoutSubviews()并添加此协议。

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

添加这样的约束。enter image description here