Swift CollectionView 获取第一个单元格

Swift CollectionView get first cell

我正在尝试使用 viewDidLoad() 从我的数据库中获取图像,然后显示图像,并突出显示第一个。

到目前为止我的代码是:

                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!)
                    }
                })

但是,对我而言,尽管存在图像并正在获取图像,但单元格始终为零,因此永远不会调用 setBorder。为什么单元格总是零?我只想在第一个单元格上设置边框。

您已将 self.photoCollectionView.reloadData() 放入异步块中,因此 let cell = self.photoCollectionView.cellForItem(at: indexPath) 将在集合视图重新加载之前立即 运行,这就是您在第一个单元格中获得 nil 的原因。

您需要确保在重新加载集合视图后,我的意思是当所有集合视图单元格都加载后,您就可以获取并执行您的操作。

或者,您可以在 cellForItemAtIndexPath 中执行此操作,如下所示...

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
}

如果您想在集合视图中加载所有图像后突出显示单元格,则需要检查集合视图的数据源。

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

cellForRow 方法中设置单元格的边框。

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
}

不是在收到响应后在viewDidLoad()中突出显示UICollectionViewCell,而是在willDisplaydelegate方法中突出显示,即

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

并在UICollectionViewCellisHighlighted属性中根据单元格高亮状态添加边框,即

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

您可以使用 isSelected 属性 相同的方法,以防您想根据选择状态更改单元格的外观。

如果您仍然遇到任何问题,请告诉我。