简体   繁体   English

UICollection 视图的无限滚动

[英]infinite scroll for UICollection view

I'm trying to have unlimited scroll in my app where i'm calling the data from an API, and at least it works to some extent, but when i scroll in the collection view, it starts populating below and it cuts of the ones on top so i can't scroll back up to them, then after populating it just skips and goes out of range, here's some code please help.我试图在我的应用程序中无限滚动,我正在从 API 调用数据,至少它在某种程度上有效,但是当我在集合视图中滚动时,它开始在下面填充并剪切那些在顶部,所以我无法向上滚动到它们,然后在填充后它只是跳过并超出范围,这里有一些代码请帮忙。

var fetchMore = false
var pageNumber = 0

var productPageString: String {
    if pageNumber == 0 {
        return "pageNumber=0&"
    } else {
        return "pageNumber=\(pageNumber)&"
    }
}

func fetchProducts() {
    fetchMore = false
    let validatedTextString = UserDefaults.standard.object(forKey: "productsSearchValue")
    let searchText = validatedTextString!
    let urlString = APIConstants.baseurl + APIConstants.products + "?searchString=\(searchText)&\(productPageString)itemsPerPage=20"
    guard let url = URL(string: urlString) else { return }
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print(error!)
            return
        }
        do {
            let jsonData = try JSONDecoder().decode(ProductSearch.self, from: data!)

            self.productSearchResults = [Products]()

            for dictionary in jsonData.data {
                let product = Products(productId: dictionary.productId, categoryId: dictionary.categoryId, storeId: dictionary.storeId, name: dictionary.name, description: dictionary.description, imageUrl: dictionary.imageUrl, price: dictionary.price)
                self.productSearchResults?.append(product)
                self.fetchMore = true
            }

        } catch let jsonError {
            print(jsonError)
        }
        }.resume()
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height

    if offsetY > contentHeight - scrollView.frame.height {
        if fetchMore {
            fetchProducts()
            pageNumber += 1
        }
    }
}

It initially shows the 20 items per page, but as the page number increases, it keeps populating with the other content in that particular page, but i can only still view 20 items per page.它最初显示每页 20 个项目,但随着页码的增加,它会不断填充该特定页面中的其他内容,但我仍然每页只能查看 20 个项目。 i want to be able to scroll back up to the previous ones and also, why does it end up taking me to the end automatically?我希望能够向上滚动到以前的那些,而且为什么它最终会自动将我带到最后? Thanks谢谢

You need to reload你需要重新加载

 for dictionary in jsonData.data {
    let product = Products(productId: dictionary.productId, categoryId: dictionary.categoryId, storeId: dictionary.storeId, name: dictionary.name, description: dictionary.description, imageUrl: dictionary.imageUrl, price: dictionary.price)
    self.productSearchResults?.append(product)
 }

 self.fetchMore = true

 DispatchQueue.main.async {
   self.collectionView.reloadData()
 }

It works now, all i had to do was remove the line现在可以了,我所要做的就是删除该行

self.productSearchResults = [Products]()

it wasn't necessary没必要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM