【问题标题】:Need UICollectionView inside a UITableViewCell to have Dynamic Height需要 UITableViewCell 内的 UICollectionView 具有动态高度
【发布时间】:2021-05-18 15:31:11
【问题描述】:

在我的UITableView 中,我有一个单独的UITableViewCell 部分。该单元格有一个水平滚动的UICollectionView

UICollectionViewCell 包含一个 UIImageView,在其下方有一个 UILabel,在该标签下方有一个 UILabel。所以这些垂直堆叠。

2 个标签最多为 2 行。 UIImageView 的大小始终相同。

标签数据会发生变化,因此在某个时间点,标签可能只占一行,而一个单元格的标签可能会占满两行。

我已尝试为 UICollectionView 添加高度限制,但目前遇到了错误。

如果标签没有得到很好的调整,但标签高度较高的单元格看起来会被向上推,而不是与所有其他单元格对齐。

我需要UITableViewCellUICollectionView 高度动态更新,以便我的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


【解决方案1】:

确认此协议 UICollectionViewDelegateFlowLayout,以及 Datasource 和 Delegate。现在使用 indexpath 为任何单元格实现 sizeForItemAt 方法。使用数据和您预期的单元格宽度计算此处的标签高度。

【讨论】:

  • 我正在这样做,但它仍然会导致上述问题。这是在上面的代码示例中。
  • 我遇到的问题是,如果有一个单元格的标签之一高于其他所有标签,那么该单元格看起来比其他单元格扩展得更多。因此,当您查看顶部时,它会高于其他顶部。它们都应该在顶部具有相同的填充。当表格单元格/集合视图高度发生变化并且标签将向下延伸时,应显示差异。
  • 澄清一件事,单元格应该扩大。但它似乎在顶部和底部扩展。我只需要它向下扩展。因此,表格视图行将与集合视图一起扩展,而标签只会向下扩展。
猜你喜欢
  • 2019-10-12
  • 2014-07-30
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
相关资源
最近更新 更多