UICollectionView + AFNetworking + AutoScroll /分页?(UICollectionView + AFNetworking + AutoScroll/Pagin

编程入门 行业动态 更新时间:2024-10-17 09:41:21
UICollectionView + AFNetworking + AutoScroll /分页?(UICollectionView + AFNetworking + AutoScroll/Pagination?)

我使用UICollectionView + AFNetworking 2从web服务异步加载图像。 它运作良好。 但是,如何实现自动滚动(当用户触及页面底部或靠近页面时,调用API来获取更多单元格)。

我见过很多例子,但似乎并不是最佳实践。 例如,一个教程提出了scrollViewDidScroll ,但每当视图滚动时都会调用它。 这是正确的实施?

PS。 我在Swift编码。

这是我的单元格创建代码:

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as UICollectionViewCell let offer = self.offers[indexPath.row] var cellImageView = cell.viewWithTag(100) as UIImageView let requestURL = NSURLRequest(URL: offer.largeImageURL) cellImageView.setImageWithURLRequest( requestURL, placeholderImage:placeholderImage, success: { (request:NSURLRequest!, response:NSHTTPURLResponse!, image:UIImage!) in cellImageView.image = image }, failure: { (request:NSURLRequest!, response:NSHTTPURLResponse!, error:NSError!) in NSLog("GET Image Error: " + error.localizedDescription) } ) return cell }

I use UICollectionView + AFNetworking 2 for loading images asynchronously from a webservice. It's working great. However, how do I implement automatic scrolling (when user reaches bottom of page, or close to it, the API is called to fetch more cells).

I've seen lots of examples but there doesn't seem to be a best practice. For example, one tutorial suggested scrollViewDidScroll, but that's called every time the view is scrolled. Is that the correct implementation?

PS. I'm coding in Swift.

Here's my cell creation code:

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as UICollectionViewCell let offer = self.offers[indexPath.row] var cellImageView = cell.viewWithTag(100) as UIImageView let requestURL = NSURLRequest(URL: offer.largeImageURL) cellImageView.setImageWithURLRequest( requestURL, placeholderImage:placeholderImage, success: { (request:NSURLRequest!, response:NSHTTPURLResponse!, image:UIImage!) in cellImageView.image = image }, failure: { (request:NSURLRequest!, response:NSHTTPURLResponse!, error:NSError!) in NSLog("GET Image Error: " + error.localizedDescription) } ) return cell }

最满意答案

你可以在scrollViewDidScroll委托方法或collectionView:cellForItemAtIndexPath:在这里你可以检测到最后一个单元格被加载)。 你需要做的是为AFNetworking调用创建一个包装器,以防止它几次加载数据(某种标志指示它已经加载)。

之后,您可以在底部插入附加的单元格,并在AFNetworking调用完成后将其删除。

You could do it in a scrollViewDidScroll delegate method or collectionView:cellForItemAtIndexPath: (here you could detect that last cell is loading). What you would need to do is to create a wrapper for AFNetworking call to prevent it from loading the data few times (some kind of flag that indicates that it's already loading).

After that you could insert additional cell with some kind of activity indicator at the bottom and remove it after AFNetworking call is finished.

更多推荐

本文发布于:2023-07-07 21:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068472.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   AFNetworking   UICollectionView   Pagination   AutoScroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!