gpt4 book ai didi

swift - 自定义 CollectionView 布局重新加载数据不起作用

转载 作者:行者123 更新时间:2023-12-02 17:01:11 24 4
gpt4 key购买 nike

创建自定义 Collection View 布局时 reloadData 不起作用

在创建自定义 Collection View 布局时,在我进行了从 api 调用新数据的无限滚动后,reloadData 不起作用

我怀疑是因为collectionview中调用的fixed items。我将如何更新它?

    protocol CustomCollectionViewLayoutDelegate: class {

func collectionView(
collectionView: UICollectionView,
heightForItemAtIndexPath indexPath: IndexPath
) -> CGFloat

}

public class CustomCollectionViewLayout: UICollectionViewLayout {

weak var delegate: CustomCollectionViewLayoutDelegate!
public var numberOfColumns: Int = 1

private var cache: [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}


public override var collectionViewContentSize: CGSize {
return CGSize(
width: self.contentWidth,
height: self.contentHeight
)
}

public override func prepare() {
if cache.isEmpty {
let columnWidth: CGFloat = self.contentWidth / CGFloat(self.numberOfColumns)
var xOffset: [CGFloat] = [CGFloat]()

for column in 0...numberOfColumns{
xOffset.append(CGFloat(column) * columnWidth)

}

var yOffset: [CGFloat] = [CGFloat](repeating: 0, count: self.numberOfColumns)

var column: Int = 0

for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
// for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
let indexPath: IndexPath = IndexPath(item: item, section: 0)
let height = delegate.collectionView(
collectionView: collectionView!,
heightForItemAtIndexPath: indexPath
)

let frame: CGRect = CGRect(
x: xOffset[column],
y: yOffset[column],
width: columnWidth,
height: height
)

let attributes: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(
forCellWith: indexPath
)

attributes.frame = frame
self.cache.append(attributes)
self.contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height

column = column < (self.numberOfColumns - 1) ? (column + 1) : 0

}
}
}

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

// Loop through the cache and look for items in the rect
for attributes in cache {
if attributes.frame.intersects(rect) {
visibleLayoutAttributes.append(attributes)
}
}

return visibleLayoutAttributes
}

public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}

}

我只需要能够使用 reloadData 来更新我的无限滚动数据

最佳答案

我建议将此功能添加到您的 CustomCollectionViewLayout

func reloadData(){
self.cache = [UICollectionViewLayoutAttributes]()
}

然后当你想重新加载你的数据时

    if let layout = self.collectionView.collectionViewLayout as? CustomCollectionViewLayout {
layout.reloadData()
}
collectionView.reloadData()

删除 if cache.isEmpty 可能会导致重用您的布局

关于swift - 自定义 CollectionView 布局重新加载数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116481/

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