我想创建一个无限滚动集合视图,因此在此代码中,每次索引路径与最后一个单元格项相等时,我将单元数加10,然后重新加载数据在集合中。该功能起作用;我可以无限滚动,但如果我停下来,然后向上或向下滚动一下,一切都是空白的。细胞没有显示。UICollectionViewCells在reloadData()后不显示()
我的理论是,它与dequeueReusableCellWithReuseIdentifier有关,它决定只显示当前在屏幕上的单元格。
View when scrolling(I加入的数字到细胞的Xcode以外)
View with the missing cells above when scrolling a bit after stopping
private let reuseIdentifier = "Cell"
private var numberOfCells = 20
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
if indexPath.row == numberOfCells - 1 {
numberOfCells += 10
self.collectionView?.reloadData()
}
cell.contentView.backgroundColor = UIColor.blueColor()
return cell
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfCells
}
}
这工作。谢谢! – noanoanoa