Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollection视图的无限滚动_Ios_Swift_Uicollectionview_Scroll Paging - Fatal编程技术网

Ios UICollection视图的无限滚动

Ios UICollection视图的无限滚动,ios,swift,uicollectionview,scroll-paging,Ios,Swift,Uicollectionview,Scroll Paging,我尝试在我的应用程序中无限滚动,我从API调用数据,至少它在某种程度上起作用,但当我在“集合”视图中滚动时,它开始在下面填充,并剪切顶部的部分,因此我无法滚动回它们,然后在填充后它只是跳过并超出范围,这里有一些代码,请帮忙 var fetchMore = false var pageNumber = 0 var productPageString: String { if pageNumber == 0 { return "pageNumber=0&"

我尝试在我的应用程序中无限滚动,我从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
        }
    }
}
它最初显示每页20个项目,但随着页码的增加,它会不断填充该特定页面中的其他内容,但我仍然只能每页查看20个项目。我希望能够滚动到上一个,而且,为什么它最终会自动将我带到最后?谢谢

您需要重新加载

 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()
 }
你需要重新加载

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

现在它可以工作了,我所要做的就是拆下电线

self.productSearchResults = [Products]()

没有必要

现在它可以工作了,我所要做的就是拆下线路

self.productSearchResults = [Products]()
没有必要