UICollectionViewController 未注册创建的自定义单元格类



我正在尝试更改单元格的颜色。我有一个名为HomeController的文件,用于布局单元格。我创建了一个名为HomePostCell的自定义单元格。我在HomeController注册了HomePostCell,但什么也没发生。单元格颜色没有变化。我在下面为这两个文件提供了我的代码。

控制器:

import UIKit
class HomeController: UICollectionViewController {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(HomePostCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
return cell
}
}

细胞:

import UIKit
class HomePostCell: UICollectionViewCell {
let photoImageView: UIImageView = {
let iv = UIImageView()
iv.backgroundColor = .blue
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(photoImageView)
photoImageView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

我希望出现五个蓝色单元格,但它们没有。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
cell.setupData()
return cell
}

最新更新