在滚动后调整单元格大小。
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
}