我有一个Horizontal
UICollectionView
在我的应用程序上,当用户在左侧拖动时到达 UICollectionView 的末尾(或接近末尾)时,我想加载更多数据。
我正在使用 Swift 4。我找到了一些 Swift 3 解决方案,但它们对我不起作用。
我当前的代码是:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.videoViewModel.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.imgImage.image = self.videoViewModel.images[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
updateVideo(data: self.videoViewModel.relatedVideos[indexPath.row])
}
您可以使用集合视图的 cellForItem 或 willDisplayItem 方法。检查是否显示最后一个单元格,然后加载数据。例如:
Swift:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}
Objective-C:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)