创建自定义集合视图布局reloadData后不起作用
创建自定义集合视图布局reloadData后,我执行了无限滚动(从api调用新数据)后不起作用
我怀疑这是因为从collectionview调用了固定项。我将如何更新它?
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更新我的无限滚动数据
答案 0 :(得分:1)
只需移除if cache.isEmpty
,就可以了
答案 1 :(得分:0)
我建议将此功能添加到您的CustomCollectionViewLayout
func reloadData(){
self.cache = [UICollectionViewLayoutAttributes]()
}
,然后当您想重新加载数据
if let layout = self.collectionView.collectionViewLayout as? CustomCollectionViewLayout {
layout.reloadData()
}
collectionView.reloadData()
删除cache.isEmpty是否可能导致布局重用