UICollectionView无限滚动

问题描述 投票:0回答:1

我搜索了这个主题,但找不到任何关于我的问题的解决方案。这是我的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单元格中显示20个项目。我忘记了什么?

ios swift uicollectionview
1个回答
1
投票

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

但是,如果我能给你一个建议,在集合视图上实现无限滚动,我会改变一些事情。

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

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

其次,当您到达数据末尾或者您已经在请求数据时,您应该尝试添加故障安全机制。否则,如果到达集合视图的底部,您将最终循环调用。以下是基于代码实现无限加载的方法

 @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)
    }

}

希望能帮助到你。

最好

© www.soinside.com 2019 - 2024. All rights reserved.