【问题标题】:CollectionView cellForItemAt never runsCollectionView cellForItemAt 从不运行
【发布时间】:2018-10-16 03:29:59
【问题描述】:

您好,我一直在寻找这个,但找不到合适的答案。 我正在尝试使用 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

}

我还将 datasource 和 collectionViewDelegate 方法设置为 self。

我一直在寻找很长一段时间才能找到答案。非常感谢任何帮助

【问题讨论】:

  • 尝试用DispatchQueue.main.async{self.collectionViewHome.reloadData()}替换self.collectionViewHome.reloadData()
  • 你永远不会用项目填充你的itemArray。请在下面查看我的答案。

标签: swift collectionview


【解决方案1】:

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

【讨论】:

  • 非常感谢 Au Ris。你是一个活生生的救星。我不敢相信我错过了这么简单的事情并为此感到头疼。再次感谢您。
  • 顺便@Dyary,如果我回答了您的问题,请将其标记为已回答。这样你就可以帮助其他可能有类似问题的人:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多