使用API进行UICollectionView分页 [英] Pagination with UICollectionView working with API
本文介绍了使用API进行UICollectionView分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个正在运行的应用程序,在其中使用 Alamofire
和 SwiftyJSON
从flicr API中获取图像。现在,我想在返回的图像中包括分页。当应用程序启动时,我将返回5张图像,并且这个数目会增加。我想通过 UICollectionView
来实现。
I have a working application where I am using Alamofire
and SwiftyJSON
to fetch images from flicr API. Now, I want to include pagination in the returned images. I am returning 5 images when the application launches and this number can increase. I want to achieve this with the UICollectionView
.
我的代码写在下面。任何帮助,将不胜感激。每页返回100个项目。
My codes are written below. Any help would be appreciated. 100 items are being returned on every page.
服务
func setData(json: JSON) -> Void {
if let photos = json["photos"]["photo"].array {
for item in photos {
let photoID = item["id"].stringValue
let farm = item["farm"].stringValue
let server = item["server"].stringValue
let secret = item["secret"].stringValue
let title = item["title"].stringValue
let imageString = "https://farm\(farm).staticflickr.com/\(server)/\(photoID)_\(secret)_b.jpg/"
//could use _n.jpg too
let flickrPhoto = FlickrPhotoModel(id: photoID, farm: farm, server: server, secret: secret, flickerImg: imageString, title: title)
flickerPhotos.append(flickrPhoto)
}
}
}
控制器
class MainVC: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
ServiceProvider.instance.getPhotos { (success) in
if success {
self.collectionView.reloadData()
}
}
}
}
extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {
let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]
cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
return cell
}
return MainCell()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ServiceProvider.instance.flickerPhotos.count
}
}
可以根据要求提供其他代码。
Further codes would be supplied on request.
推荐答案
请尝试使用此代码。
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if isGetResponse {
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height)
{
if totalArrayCount! > self.arrImage.count{
isGetResponse = false
activityIndiator?.startAnimating()
self.view.bringSubview(toFront: activityIndiator!)
pageIndex = pageIndex + 1
print(pageIndex)
self.getProductListing(withPageCount: pageIndex)
}
}
//LOAD MORE
// you can also add a isLoading bool value for better dealing :D
}
}
这篇关于使用API进行UICollectionView分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文