簡體   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