xcode Swift 4 UICollectionView 检测滚动结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48705430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 4 UICollectionView detect end of scrolling
提问by Rexhin Hoxha
I have an Horizontal
UICollectionView
on my app and I want to load more data when the user reaches the end (or nearly to the end) of UICollectionView while dragging on the left.
我的Horizontal
UICollectionView
应用程序上有一个,我想在用户在左侧拖动时到达 UICollectionView 的末尾(或接近末尾)时加载更多数据。
I'm using Swift 4. I found some Swift 3 solutions but they do not work for me.
我正在使用 Swift 4。我找到了一些 Swift 3 解决方案,但它们对我不起作用。
My current code is:
我目前的代码是:
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])
}
回答by Fayza Nawaz
You may use cellForItem or willDisplayItem methods of collection view. Check if the last cell is being displayed, and load your data. For example:
您可以使用集合视图的 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:
目标-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
}
}
回答by Ilya Kharabet
Implement method scrollViewDidScroll
of UIScrollViewDelegate
:
实现方法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
}
}
回答by iVarun
Take one bool variable and one integer variable for page number like this:
取一个布尔变量和一个整数变量作为页码,如下所示:
var isDataLoading = false
var pageCount:Int = 1 // Pass this page number in your api
in cellForItemAt method add below code:
在 cellForItemAt 方法中添加以下代码:
if !isDataLoading && indexPath.row == videoViewModel.count - 1 {
isDataLoading = true
pageCount += 1
// add you api call code here with pageCount
}
once you get your data from api set isDataLoading bool to false like below:
一旦你从 api set isDataLoading bool 到 false 获取数据,如下所示:
self.isDataLoading = false
回答by Zain Shahzad
This is simple login which will go down to last index...!!!
这是一个简单的登录,它将下降到最后一个索引......!!!
func ScrollEnd() {
let lastSectionIndex = (self.collectionView?.numberOfSections)! - 1
let lastItemIndex = (self.collectionView.numberOfItems(inSection: lastSectionIndex)) - 1
let index = IndexPath(item: lastItemIndex, section: lastSectionIndex)
if messages.count != 0{
self.collectionView!.scrollToItem(at: index, at: UICollectionView.ScrollPosition.bottom, animated: false)
} }
回答by Krishna Srivastava
Add this in your collectionView willDisplayCell delegate
将此添加到您的 collectionView willDisplayCell 委托中
if (indexPath.row == dataSource.count - 1 ) {
//it's your last cell
//Load more data & reload your collection view
}