我试图在演示应用程序中实现分页。我正在使用UICollectionView来使用SDWebImage从API显示大量图像。而API支持这样的分页:分页在分页视图中如何工作?
我的问题是如何将这个nextPage的图像显示到我的collectionview?
{
"meta":{
"code":200
},
"data":{ },
"pagination":{
"total":86,
"totalPages":3,
"page":1,
"nextPage":2,
"nextPageUrl":"http://.............?page=2"
}
}
而我的目标是,以显示此nextPageUrl的PIC到的CollectionView。 这里是我的代码:
class StoreViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
@IBOutlet var MyStoreCollectionView: UICollectionView!
var alldata: [PopulerMagazalarData]?
var indexPath: IndexPath?
var storeData : [PopulerMagazalarData] = []
let pagenumber = 1
override func viewDidLoad() {
super.viewDidLoad()
if let indexPath = self.indexPath, let storeData = self.alldata?[indexPath.row] {
let storeusername = storeData.username
GetDataFromUrl(from: "https://............./\(storeusername!)?page=\(pagenumber)")
}
}
而我得到的数据,从URL的乐趣...
func GetDataFromUrl(from:String){
Alamofire.request(from, method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
self.storeData = [PopulerMagazalarData]()
//...Creating Data Obj.
let data = PopulerMagazalarData()
let username = json["data"]["store"]["user"]["username"].string
let userpic = json["data"]["store"]["user"]["profilePicture"].string
let productsCount = json["data"]["store"]["productsCount"].int
let description = json["data"]["store"]["description"].string
let followedby = json["data"]["store"]["user"]["counts"]["followedBy"].int
let count:Int? = json["data"]["products"].array?.count
if let ct = count {
for index in 0...ct-1{
let images = json["data"]["products"][index]["images"]["standart"]["url"].string
data.img1 = images
self.storeData.append(data)
}
}
//*****************
data.username = username
data.profilPic = userpic
data.producsCount = productsCount
data.desc = description
data.followedby = followedby
//******************
self.storeData.append(data)
// for refresh collecitonView
self.refresh_now()
case .failure(let error):
print(error)
}
}
}
//...CollectionView ReloadData func...
func refresh_now(){
DispatchQueue.main.async (
execute:
{
self.MyStoreCollectionView.reloadData()
}
)
}
,这是我的CollectionView基金:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return 1
}
我更喜欢使用默认值。我的意思是footerview/UICollectionElementKindSectionFooter –
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 您可以使用此方法检查最后一个单元格。 –