如何根据UITableViewCell中的内容调整水平UICollectionView高度

Ser*_*can 5 uitableview ios uicollectionview uicollectionviewcell swift

我正试图放入UICollectionView一个UITableViewCell.我想用UICollectionViewCells分页在s中显示注释.注释中可能有多行,所以UICollectionView如果注释标签中有多行,我想调整大小.

这是该问题的演示项目.如果您能解决这个演示中的问题,我将非常感激.

细胞看起来像这样.

在此输入图像描述

我尝试了一些答案,但我无法取得任何进展.你能告诉我我需要一步一步做什么吗?如果你能分享,演示会好得多.谢谢.

Har*_*rsh 5

好的..所以我遇到了同样的问题...我通过以下步骤修复了它...

创建集合视图的高度约束出口,您将根据拥有的动态内容设置该高度约束出口。

`@IBOutlet private(set) weak var collectionViewHeightConstraint: NSLayoutConstraint!`
Run Code Online (Sandbox Code Playgroud)

找到最大的内容模型(在您的情况下将是最大的评论)并找到最大集合视图单元格的高度。这可以通过以下方法来完成。

private func getSizingCellHeight(for item: T.YourModel) -> CGFloat {
    guard let sizingCell = sizingCell else {
        return 0
    }

    sizingCell.frame.size.width = collectionViewItemWidth // This would be the width your collection view has
    sizingCell.prepareForReuse()

    // This is the method inside of your collection view cell, which lays out the content of the cell 
    sizingCell.configure(with: item)
    sizingCell.layoutIfNeeded()

    var fittingSize = UILayoutFittingCompressedSize
    fittingSize.width = collectionViewItemWidth

    let size = sizingCell.contentView.systemLayoutSizeFitting(
        fittingSize,
        withHorizontalFittingPriority: .required,
        verticalFittingPriority: .defaultLow
    )

    return size.height
}
Run Code Online (Sandbox Code Playgroud)

运行上述方法将为您提供最大单元格的高度,您可以将其用于 collectionView 的高度约束。

private func setCollectionViewHeight() {
    var largestHeight: CGFloat = 0

    //items would be your datasource for the collection view
    let items: [YourModel] = .... with some values
    if let items = items {
        for item in items {
            let height = getSizingCellHeight(for: item)
            if height > largestHeight {
                largestHeight = height
            }
        }
    }

    collectionViewHeightConstraint?.constant = largestHeight
}
Run Code Online (Sandbox Code Playgroud)

这是我的项目中的实际工作代码。

从我最后的记忆来看,我遵循了查理的文章

这篇文章在Github上还有一个工作演示


ISS*_*ISS -1

对于根据内容长度动态调整单元格大小,您基本上必须进行一些近似。这是我的解决方案。它需要进行几次试验才能使计算正确。现在请耐心听我说!

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if let user = datasource?.item(indexPath) as? User {
        let approximateWidthBioTextView = view.frame.width - 10 - 60 - 10
        let size = CGSize(width: approximateWidthBioTextView, height: 1000)
        let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]

        let estimatedFrame = NSString(string: user.bio).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)

        return CGSize(width: view.frame.width, height: estimatedFrame.height + 73)
    }

    return CGSize(width: view.frame.width, height: 200)
}
Run Code Online (Sandbox Code Playgroud)

本质上,您需要更改 sizeForItem 函数并进行一些近似。我的内容是由 a 的实例决定的User Class。因此,请确保在开始引用单元格内的内容之前。就我而言,它是需要显示的用户。简而言之,我的解决方案根据 user.bioText 获取单元格的高度。因为这是我的单元格中唯一改变大小的元素。所以请记住这一点。

首先,您必须使用以下方法计算 collectionView。就我而言,它是框架的宽度减去边距(10、60 和 10)。请记住,正确执行此操作非常重要。

其次,为了估计单元格的高度,我们需要将其包装在一个具有一些需要填写的属性的矩形中。在size常量中,我们故意赋予一个高值,以便系统获取它真正需要的东西。现在,对于属性,如果您的内容是字符串(如我的情况),请确保您具有相同的字体大小。

最后,在返回部分return CGSize(width: view.frame.width, height: estimatedFrame.height + 73),我们仍然需要进行一些更正,因为文本有一些内部填充(如果我可以这么说),除非您更正,否则您的单元格高度将无法正确显示。确保使用最后一个值estimatedFrame.height + 73(在我的例子中为 73),直到达到单元格高度完美动态分配的程度。这是唯一的办法。它肯定对我有用。

顺便说一句,如果您的代码看起来有点不同(不过也没有太大不同),请不要担心。我用的是框架,但是计算原理是一样的。如果您问自己,73 值从何而来,那是因为在我需要在每个单元格中显示的个人简介文本的顶部,我有用户名和用户名标签,它们都是 20 中的 8,即 40总计 33 是​​它们之间的差距或边距。只要你给它最小值,它就应该起作用。如果你给它一个太高的值,它并不会真正影响结果。所以实验吧!