CollectionView cellForItemAt永远不会运行

时间:2018-10-16 03:30:00

标签: swift collectionview

嗨,我一直在寻找这个东西,却找不到合适的答案。 我正在尝试使用使用API​​在线接收的数据填充collectionView。 返回了数据,但是collectionView cellForItemaAt从未运行,因为当我使用打印语句时,什么都不会显示。

我不知道是什么问题,我看了看这些拖链,但它们没有帮助:

collectionView cellForItemAt not being called

cellForItemAt is not calling in collectionView

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。

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

1 个答案:

答案 0 :(得分:0)

在您的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()派发到主线程,就像评论中提到的人一样。