gpt4 book ai didi

ios - 分页在 collectionview 中是如何工作的?

转载 作者:行者123 更新时间:2023-11-28 08:15:36 24 4
gpt4 key购买 nike

我正在尝试在演示应用程序中实现分页。我正在使用 UICollectionView 来显示来自使用 SDWebImage 的 API 的大量图像。 API 支持这样的分页:

我的问题是如何将下一页的图像显示到我的收藏 View 中?

{  
"meta":{
"code":200
},
"data":{ },
"pagination":{
"total":86,
"totalPages":3,
"page":1,
"nextPage":2,
"nextPageUrl":"http://.............?page=2"
}
}

我的目标是向 Collection View 显示此 nextPageUrl 的图片。这是我的代码:

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
}

最佳答案

check my this answer首先,当 footerview 出现时,将页脚 View 添加到您的 collectionview 进行网络调用,将新数组附加到先前的数组并重新加载您的 collectionview

关于ios - 分页在 collectionview 中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com