【发布时间】:2021-05-18 15:31:11
【问题描述】:
在我的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, font: UIFont(name: "Font", 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
}
}
【问题讨论】:
-
从 sizeForItemAt 方法为 collectionview 发送计算出的高度,并使用大于或等于填充的约束来使您的单元格在 collectionview 中居中
-
关于如何发送计算出的高度的任何其他上下文?
标签: ios swift uitableview dynamic uicollectionview