gpt4 book ai didi

ios - UICollectionView 无限滚动

转载 作者:行者123 更新时间:2023-11-28 13:51:31 34 4
gpt4 key购买 nike

我搜索了这个主题,但找不到任何解决我问题的方法。这是我的 loadData 函数可以从 JSON 对象获取数据

 @IBOutlet weak var collectionVitrin: UICollectionView!

var vitrinDecodePost = [VitrinDecode]() // Decodable

func loadDatas(limit : Int) {

guard let url = URL(string: "my Url") else { return }
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
let paramString = "limit=\(limit)"
request.httpBody = paramString.data(using: String.Encoding.utf8)

let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
guard let _:NSData = data as NSData? , let _:URLResponse = response, error == nil else {

print("Mistake")
return
}

guard let data = data else { return }
do {

let abc = try JSONDecoder().decode([VitrinDecode].self, from: data)
self.vitrinDecodePost = abc

DispatchQueue.main.async {

self.collectionVitrin.reloadData()
}

} catch { print(error)}

}
task.resume()
}

我在我的 ViewDidLoad 中运行这个函数:

var deger : Int = 10

override func viewDidLoad() {
super.viewDidLoad()
loadData(limit: deger)
}

当滚动结束时我想添加更多数据所以我添加了 willDisplay 函数

func collectionView(_ collectionView: UICollectionView, 
numberOfItemsInSection section: Int) -> Int {

return self.vitrinDecodePost.count
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

let lastindex = self.vitrinDecodePost.count - 1
if indexPath.row == lastindex{

loadDatas(limit: 20)
self.collectionVitrin.reloadData()


}

}

当页面加载时,我的单元格中显示 10 个项目,但当我到达页面末尾(最后一个索引)时,collectionview 不显示任何内容。它必须在我的 collectionview 单元格中显示 20 个项目。我忘记了什么?

最佳答案

您应该删除 willDisplay 函数中 LoadDatas(limit: 20) 之后调用的 self.collectionVitrin.reloadData()

但是如果我能给你一个在 Collection View 上实现无限滚动的建议,我会改变一些东西。

首先,我建议您更改 api 调用。您应该保持从每个 api 调用发回的元素数量的限制,但也要实现一个偏移量变量。这样效率更高。

在您的情况下,您的第二个 api 调用只是再次请求相同的对象,但请求中的限制更大。您基本上每次都要求相同的数据。添加偏移量使您每次只能请求新数据。此偏移量应该是您已有的数据量。

其次,当您到达数据末尾或已经在请求数据时,您应该尝试添加故障安全机制。否则,如果您到达收藏 View 的底部,您将最终循环通话。这是我将如何根据您的代码实现无限加载

 @IBOutlet weak var collectionVitrin: UICollectionView!
var limitPerCall: Int = 10
var isLoadindNewData = false
var shouldLoadMoreData = true

var vitrinDecodePost = [VitrinDecode]() // Decodable

func loadDatas(limit : Int, offset: Int) {

guard let url = URL(string: "my Url") else { return }
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
let parameters = ["limit": "\(limit)", "offset": "\(offset)"] as Dictionary<String, String>
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody

let task = session.dataTask(with: request as URLRequest) { [weak self]
(data, response, error) in
guard let strongSelf = self, let _:NSData = data as NSData? , let _:URLResponse = response, error == nil else {

print("Mistake")
return
}

/// No More data to gather stop making api calls
guard let data = data else {
strongSelf.shouldLoadMoreData = false
return
}
do {

let abc = try JSONDecoder().decode([VitrinDecode].self, from: data)
strongSelf.vitrinDecodePost.append(contentsOf: abc)

//// Reload the new data and and indicates that api call can be
made again
DispatchQueue.main.async {
strongSelf.isLoadingNewData = false
strongSelf.collectionVitrin.reloadData()
}

} catch { print(error)}

}
task.resume()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection
section: Int) -> Int {

return self.vitrinDecodePost.count
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell:
UICollectionViewCell, forItemAt indexPath: IndexPath) {

let lastindex = self.vitrinDecodePost.count - 1
if indexPath.row == lastindex && !isLoadindNewData && shouldLoadMoreData {
/// Forbids multiple api calls to happen at the same time
isLoadindNewData = true
loadDatas(limit: limitPerCall, offset: vitrinDecodePost.count)
}

}

希望对您有所帮助。

最佳

关于ios - UICollectionView 无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539326/

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