Rex*_*xha 7 xcode ios uicollectionview swift swift4
我有一个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])
}
Run Code Online (Sandbox Code Playgroud)
实现方法scrollViewDidScroll的UIScrollViewDelegate:
var isLoading: Bool = false
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffsetX = scrollView.contentOffset.x
if contentOffsetX >= (scrollView.contentSize.width - scrollView.bounds.width) - 20 /* Needed offset */ {
guard !self.isLoading else { return }
self.isLoading = true
// load more data
// than set self.isLoading to false when new data is loaded
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用集合视图的cellForItem或willDisplayItem方法.检查是否显示最后一个单元格,然后加载数据.例如:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12262 次 |
| 最近记录: |