繁体   English   中英

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

但是,对我来说,尽管存在图像并正在获取图像,但是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)
          }
     }
}

收到响应后, UICollectionViewCellviewDidLoad()中突出显示UICollectionViewCell ,而应在willDisplay delegate方法中突出显示它,即

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

而在补充细胞的边界isHighlighted财产UICollectionViewCell根据小区光亮状态,即

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

如果要基于选择状态更改单元格的外观,则可以以相同方式使用isSelected属性。

让我知道您是否仍然遇到任何问题。

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM