如何修复:UICollection视图cellForItemAt不执行

时间:2019-07-09 21:42:34

标签: arrays swift uicollectionview

我有这个collectionView方法的设置,要为完成这里的计数后返回的“项目”数组中的每个项目创建一个单元格。

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

在类文件的开头,items数组最初为空var items: [String] = []。在我的响应作为JSON响应对象从服务器返回后,该变量将充满数据。该对象包含带有数据的“ items”数组,然后像这样设置

 let data = data as! Data
                let decoder = JSONDecoder()
                let dataTest = try? decoder.decode(ActiveData.self, from: data)

                //Fill vars with the correct info
                self.winCount = dataTest?.win ?? self.winCount
                self.lossCount = dataTest?.loss ?? self.lossCount
                print("DEV: winCount \(self.winCount), lossCount \(self.lossCount)")

                //Sets win / loss text after vars are filled
                self.win.text = "\(self.winCount)"
                self.loss.text = "\(self.lossCount)"

                self.items = dataTest?.profilePictures ?? self.items
                print("DEV: items \(String(describing: self.items))")
                print("DEV: dataTest.profilePictures: \(String(describing: dataTest?.profilePictures))")

                self.challengeNames = dataTest?.challengeNames ?? self.challengeNames
                print("DEV: challengeNames \(String(describing: self.challengeNames))")
                print("DEV: dataTest.challengeNames: \(String(describing: dataTest?.challengeNames))")

提到的collectionView方法根本不响应正在填充的数据。.我已经确认带有“ profilePictures”数组的JSON响应具有应有的数据。

提到了

collectionView方法:

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

            cell.profilePicture.layer.masksToBounds = true

        if items != [] {
            cell.profilePicture.playPortalProfilePic(forImageId: items[indexPath.item], { error in
                if let error = error {
                    print("Error requesting profile pic: \(String(describing: error))")
                }
            }
            )
        } else {
            print("items array was empty, value: \(items)")
        }


        cell.backgroundColor = UIColor.cyan
        cell.layer.borderColor = UIColor.black.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 8

        return cell
    }

0 个答案:

没有答案