CollectionView cellForItemAt 从不运行

CollectionView cellForItemAt never runs

您好,我一直在寻找这个,但找不到合适的答案。 我正在尝试使用 API 使用在线接收的数据填充 collectionView。 数据已返回,但 collectionView cellForItemaAt 从未运行,因为当我使用 print 语句时,不会显示任何内容。

我不知道问题出在哪里,我查看了这些拖车链接,但它们没有帮助:

cellForItemAt never called in a class extends UICollectionViewController

这是 cellForItemAt 方法:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {

        collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! ItemCollectionViewCell
            cell.ItemNameLabel.text = itemArray[indexPath.row].name

            print(itemArray[indexPath.row].name)

            cell.priceLable.text = itemArray[indexPath.row].price + " " + itemArray[indexPath.row].currency

            guard let url : URL = URL(string:  itemArray[indexPath.row].image) else {return cell}

            cell.imageView.sd_setShowActivityIndicatorView(true)
            cell.imageView.sd_setIndicatorStyle(.gray)
            cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named:"placeholderImage"), options: .refreshCached, completed: nil)


            return cell

        }

这是我用来检索数据的方法:

 func getAllItemsApiMethod()
    {
        itemArray.removeAll()
        if Reachability.sharedInstance.connectedToNetwork()
        {
            if searchShops == true {
                PostDict = ["page" : pageNumber ,"text" : searchKeyword,"searchShop" : "1"]
            }else{
                   PostDict = ["page":"1","text":searchKeyword]
            }

            print(PostDict)

            StartIndicator()
            WebServices.getItemsMethod(url: itemsUrl, parameters: PostDict) { (JsonResponse) in
                print(JsonResponse)
                StopIndicator()
                let json : JSON = JSON(JsonResponse)


               self.updateItemData(json: json)



                print(json)
            }
        }
        else
        {
            FTIndicator.showToastMessage(Constant.NoInternet)
        }

    }


    func updateItemData (json : JSON) {
        let item = Item()

        for i in 0...json["data"].count - 1{
            item.name = json["data"][i]["title_ku"].stringValue
            item.price = json["data"][i]["price"].stringValue
            item.image = json["data"][i]["image"].stringValue
            item.currency = json["data"][i]["currency"].stringValue


        }

      //  useItemsCell = true
         self.collectionViewHome.reloadData()
    }

这是我用来调用 getAllItemsAPI 的方法:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {


        guard let searchKeyword = txtFldSearch.text else {return false}

        getAllItemsApiMethod()


        collectionViewHome.reloadData()

        self.view.endEditing(true)
        txtFldSearch.text = ""


        return true

    }

这是 numberOfItems 方法:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return itemArray.count

}

我还将数据源和 collectionViewDelegate 方法设置为 self。

我一直在寻找答案。非常感谢任何帮助

在您的 updateItemData 方法中,您正在解析 JSON,但我看不到您会将这些项目添加到任何数据源对象。您只是遍历集合但不对其进行任何操作,因此当您重新加载 collectionView 时,您的数据源仍然是空的。确保将项目添加到 itemArray:

func updateItemData (json : JSON) {

    itemArray = []
    for i in 0...json["data"].count - 1{
        let item = Item()
        item.name = json["data"][i]["title_ku"].stringValue
        item.price = json["data"][i]["price"].stringValue
        item.image = json["data"][i]["image"].stringValue
        item.currency = json["data"][i]["currency"].stringValue
        itemArray.append(item)
    }

  //  useItemsCell = true
     self.collectionViewHome.reloadData()
}

此外,如果您的 API 回调发生在非主线程上,请确保将 collectionViewHome.reloadData() 分派给主线程,就像评论中提到的那样。