gpt4 book ai didi

ios - 使用自定义 Collection View 布局快速配置动态单元格高度

转载 作者:行者123 更新时间:2023-11-30 11:10:24 27 4
gpt4 key购买 nike

我有一个自定义 Collection View 布局,它从下往上加载消息应用程序的单元格,但我在实现动态单元格高度时遇到了麻烦。我相信我连接的用于检索动态单元格高度的协议(protocol)和委托(delegate)适用于每个单元格(这是我通过打印每个 indexPath.item 的高度推断出来的),除非在 collectionView 加载单元格高度时不是他们应该有的高度。它们都只是一个静态高度(不是 var cellHeight 预设的 80)并且只显示一行文本。

 protocol InvertedStackProtocol: class {
func collectionView(_ collectionView: UICollectionView, heightForCellAtIndexPath indexPath: IndexPath) -> CGFloat
}

class InvertedStackLayout: UICollectionViewLayout, UICollectionViewDelegateFlowLayout {

var cellHeight: CGFloat = 80

weak var delegate: InvertedStackProtocol!

override func prepare() {

guard let collectionView = self.collectionView else {return}

for item in 0 ..< collectionView.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item, section: 0)
let height = delegate.collectionView(collectionView, heightForCellAtIndexPath: indexPath)
print (height, indexPath.item)
cellHeight = height
}

}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttrs = [UICollectionViewLayoutAttributes]()

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numberOfSectionItems = numberOfItemsInSection(section) {
for item in 0 ..< numberOfSectionItems {
let indexPath = IndexPath(item: item, section: section)
let layoutAttr = layoutAttributesForItem(at: indexPath)

if let layoutAttr = layoutAttr, layoutAttr.frame.intersects(rect) {
layoutAttrs.append(layoutAttr)
}
}
}
}
}

return layoutAttrs
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let layoutAttr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
let contentSize = self.collectionViewContentSize

layoutAttr.frame = CGRect(
x: 0, y: contentSize.height - CGFloat(indexPath.item + 1) * cellHeight,
width: contentSize.width, height: cellHeight)

return layoutAttr
}

func numberOfItemsInSection(_ section: Int) -> Int? {
if let collectionView = self.collectionView,
let numSectionItems = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: section)
{
return numSectionItems
}

return 0
}

override var collectionViewContentSize: CGSize {
get {
var height: CGFloat = 0.0
var bounds = CGRect.zero

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numItems = numberOfItemsInSection(section) {
height += CGFloat(numItems) * cellHeight
}
}

bounds = collectionView.bounds
}

return CGSize(width: bounds.width, height: max(height, bounds.height))
// return CGSize(width: bounds.width, height: height) do this for more exact fit of collection view
}
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
if let oldBounds = self.collectionView?.bounds,
oldBounds.width != newBounds.width || oldBounds.height != newBounds.height
{
return true
}

return false
}
}

最佳答案

使用此方法可以更改 UICollectionViewCell 的高度。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 125.0, height: 175.0)
}

关于ios - 使用自定义 Collection View 布局快速配置动态单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52249487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com