如何解决需要 UITableViewCell 内的 UICollectionView 具有动态高度
在我的 UITableView
中,我有一个单独的 UITableViewCell
部分。该单元格具有水平滚动的 UICollectionView
。
UICollectionViewCell
包含一个 UIImageView
,其下方有一个 UILabel
,该标签下方有一个 UILabel
。所以这些垂直堆叠。
2 个标签最多为 2 行。 UIImageView
的大小始终相同。
标签数据会发生变化,所以在某个时间点可能会有只占一行的标签,而一个单元格可能有填满两行的标签。
我曾尝试为 UICollectionView
添加高度约束,但目前遇到了错误。
如果标签没有得到很好的调整,但是标签高度较高的单元格看起来会被向上推,而不是与所有其他单元格对齐。
我需要 UITableViewCell
和 UICollectionView
高度动态更新,以便我的 UICollectionViewCells
行保持对齐。这意味着,如果有意义的话,图像将保持对齐并且标签保持相对对齐。
对于 UITableViewCell
xib,我在顶部有一个标签,然后在其下方是 UICollectionView
,它具有顶部、前导、尾部和底部约束,高度约束连接到出口.
如果需要任何其他信息来提供帮助,请告诉我。
这是我尝试过的:
// For the UITableViewCell
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "customCollectionViewCell",for: indexPath) as? customCollectionViewCell {
let collectionItem = UINib.init(nibName: "collectionItem",bundle: nil)
cell.myCollectionView.register(collectionItem,forCellWithReuseIdentifier: "collectionItem")
cell.myCollectionView.dataSource = self
cell.myCollectionView.delegate = self
cell.myCollectionView.reloadData()
// This seems to be causing some issues..
let height = cell.myCollectionView.collectionViewLayout.collectionViewContentSize.height
cell.collectionViewHeightConstraint.constant = height
cell.myCollectionView.setNeedsLayout()
cell.myCollectionView.layoutIfNeeded()
return cell
} else {
return UITableViewCell()
}
}
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
// The over all height shouldn't be more than this
return 300.0
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
// Ideally I would like to just use this vs. setting a height for the row
return UITableView.automaticDimension
}
// For setting the UICollectionViewCell Height
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let myData = //getMyData,(myData.count - 1) >= indexPath.row else {
return CGSize.zero
}
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionItem",for: indexPath) as? collectionItem else { return CGSize.zero }
let theData = myData[indexPath.row]
let h = calculateHeight(titleString: theData.title ?? "",subTitleString: theData.description ?? "")
cell.layoutIfNeeded()
return CGSize(width: 160.0,height: h)
}
func calculateHeight(titleString: String,subTitleString: String) -> CGFloat {
let imageHeight: CGFloat = 160.0
let spaceBetweenImageAndLabels: CGFloat = 4.0
let titleHeight = heightForView(text: titleString,font: UIFont(name: "Font",size: 14.0)!,width: 160.0)
let subTitleHeight = heightForView(text: subTitleString,size: 12.0)!,width: 160.0)
let height = imageHeight + spaceBetweenImageAndLabels + titleHeight + subTitleHeight
return height
}
func heightForView(text:String,font:UIFont,width:CGFloat) -> CGFloat {
let label:UILabel = UILabel(frame: CGRect(x: 0,y: 0,width: width,height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 2
label.lineBreakMode = .byTruncatingTail
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
}
解决方法
确认此协议 UICollectionViewDelegateFlowLayout,以及 Datasource 和 Delegate。现在使用 indexpath 为任何单元格实现 sizeForItemAt 方法。在这里使用数据和您期望的单元格宽度计算标签高度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。