1

I have a UIColelctionView cell that should contain users names located in a firebase database.

First I referenced the custom cell using:

let f = FriendCell()

In cellForItemAt indexPath, I define each cell and reference the array from which the data is coming from. Then I reference the individual user the particular cell will get its data from.

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userResultCellId, for: indexPath) as! FriendCell

    let user = users[indexPath.row]

    f.nameLabel.text = user.name
    print(f.nameLabel.text!)

    return cell
}

When I run this, the console print(f.nameLabel.text!) actually prints the user names correctly, however they don't seem to pass into the cell correctly.

What I believe is happening is each cell is getting set up before the data is downloaded and the namelabel is returning nil because there isn't a value there. But what's weird is that when I print the value it returns the names correctly. Any suggestions?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Stefan
  • 908
  • 1
  • 11
  • 33

1 Answers1

4

You do not need the line:

let f = FriendCell()

Get rid of that. Then in your cellForItemAt, you need to set the properties of cell, not f.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userResultCellId, for: indexPath) as! FriendCell

    let user = users[indexPath.row]
    cell.nameLabel.text = user.name

    return cell
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579