为什么只更新第一个单元格中的图像,而 uicollectionview 中的其余单元格未更新。 ios swift

Why is only image in first cell updated while the rest of the cells in uicollectionview not updated. ios swift

我正在尝试在顶部栏中添加 3 个图标。

  import UIKit

  class SectionHeader: UICollectionReusableView {

private let telButtonCellId = "TelButtonCell"
private let searchBarCellId = "SearchBarCell"
private let notificationButtonCellId = "NotificationButtonCell"

// MARK: - Properties

@IBOutlet weak var collectionView: UICollectionView!

// MARK: - Initialization



override func awakeFromNib() {
    super.awakeFromNib()
    self.collectionView.register(UINib(nibName:notificationButtonCellId, bundle: nil), forCellWithReuseIdentifier: notificationButtonCellId)

    self.collectionView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width*0.2) // this is need to set the size of the collection view
    self.collectionView.delegate = self
    self.collectionView.dataSource = self

}


extension SectionHeader : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: notificationButtonCellId, for: indexPath) as! NotificationButtonCell
    var image = UIImage(named: "globe")
    cell.imageView.image=image?.addImagePaddingShift(x: 20 , y: 20,shiftX: 0, shiftY: 10)
    cell.imageView.contentMode = .scaleAspectFit
    cell.imageView.bounds=cell.bounds
    cell.imageView.center = cell.center;
    cell.backgroundColor = UIColor.lightGray

    return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let frameWidth = UIScreen.main.bounds.width
    let frameHeight = UIScreen.main.bounds.width*0.2

    return CGSize(width: frameWidth*0.15, height: frameHeight*1.0)
}

因此只有第一个单元格会显示地球。 尽管我可以更改背景颜色,但其余 2 个单元格都是空的。

SectionHeader 是笔尖

imageView 是单元格的子视图,它需要像我看到的那样大小。所以它需要将它的框架(它的位置相对于它的超级视图坐标系 - 单元格)设置为原点(0,0)的单元格的高度和宽度。单元格的边界提供了这些维度,因此答案可以是

cell.imageView.frame = cell.bounds

或更好

cell.imageView.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)