0

I have this collectionView method setup to, make a cell for each item in my "items" array returned after the count done here.

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

the items array is initially empty var items: [String] = [] at the start of the class file. This variable gets filled with data after my response comes back from a server as a JSON response object. That object contains the "items" array with data that is then set like so

 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))")

The collectionView method mentioned is NOT responding at all to the data being filled.. I have already confirmed the JSON response with the "profilePictures" array has data like it should.

collectionView method mentioned:

   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
    }
Joshua
  • 481
  • 6
  • 21
  • 1
    Have you set the data source and delegate of the collection view to your view controller? Also, if the data is obtained asynchronously, you might need to reload the collection view. – Chris Jul 09 '19 at 21:49
  • 1
    @Chris you should turn that into an answer. I think you nailed it. OP needs to reload data after the server response populates the items variable. OP, you need to give us some context on when that second block of code is running. – GntlmnBndt Jul 09 '19 at 22:16
  • 1
    @Chris you nailed it! The data need to be reloaded, thanks for the help guys. This was my first time working with a collectionView. Now i know! appreciate it – Joshua Jul 10 '19 at 13:48
  • 1
    @GntlmnBndt if it wasn't marked as a duplicate the answer would go to chris. Thanks for the helpful comments! – Joshua Jul 10 '19 at 13:49
  • @JoshuaPaulsen No problem! It was marked as duplicate before I got time to type an answer. Glad you got sorted though! :) – Chris Jul 10 '19 at 13:51
  • 1
    @Chris nice attitudes are always appreciated on Stack Overflow because they seem to be hard to find / run into! Thanks again – Joshua Jul 10 '19 at 15:45

0 Answers0