AlexK
AlexK

Reputation: 396

Swift CollectionView get first cell

I am trying on my viewDidLoad() to fetch images from my DB and then present the images, and highlight the first one.

My code thus far is:

                profile?.fetchImages(onComplete: { (errors, images) in
                    for error in errors {
                        print("Error", error)
                    }
                    for imageMap in images {
                        self.photos.append(imageMap.value)
                    }
                    if self.photos.count < self.MAX_PHOTOS {
                        self.photos.append(self.EMPTY_IMAGE)
                    }
                    DispatchQueue.main.async {
                        self.photoCollectionView.reloadData()
                    }
                    self.updateSlideshowImages()
                    let indexPath = IndexPath(row: 0, section: 0)
                    let cell = self.photoCollectionView.cellForItem(at: indexPath)
                    if cell != nil {
                        self.setBorder(cell!)
                    }
                })

However, for me cell is always nil, despite images existing and being fetched, and thus the setBorder is never called. Why is the cell always nil? I just want to set the border on the first cell.

Upvotes: 2

Views: 5961

Answers (3)

PGDev
PGDev

Reputation: 24341

Instead of highlighting the UICollectionViewCell in viewDidLoad() after receiving the response, highlight it in willDisplay delegate method, i.e.

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
    if indexPath.row == 0
    {
        cell.isHighlighted = true
    }
}

And add border of cell in the isHighlighted property of UICollectionViewCell according to the cell highlight status, i.e.

class CustomCell: UICollectionViewCell
{
    override var isHighlighted: Bool{
        didSet{
            self.layer.borderWidth = self.isHighlighted ? 2.0 : 0.0
        }
    }
}

You can use isSelected property the same way in case you want to change appearance of the cell based on the selection status.

Let me know if you still face any issue.

Upvotes: 2

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

Set your cell's border in cellForRow method.

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) as! YourCustomCell

    if indexPath.row == 0 {
         self.setBorder(cell)
    }

    return cell
}

Upvotes: 1

Mahendra
Mahendra

Reputation: 8904

You have placed self.photoCollectionView.reloadData() in async block so the let cell = self.photoCollectionView.cellForItem(at: indexPath) will run immediately before collection view reload, thats the reason you are getting nil for first cell.

You need to make sure that after collection view reload, I mean when all collection view cells are loaded then you can get and do your operations.

Alternatively, you can do this in cellForItemAtIndexPath like below...

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier.jobCollectionViewCellIdentifier, for: indexPath) as! <#Your UICollectionViewCell#>

    if indexPath.section == 0 && indexPath.row == 0 {
          //highlight cell here
      }

    return cell
}

And if you want to highlight cell after all images load in collection view then you need to check for datasource of collection view.

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

     let cell = ...

     if indexPath.row == arrImages.count-1 {

          let newIndexPath = IndexPath(row: 0, section: 0)
          if let cellImg = self.photoCollectionView.cellForItem(at: newIndexPath) {
              self.setBorder(cellImg)
          }
     }
}

Upvotes: 2

Related Questions