我正在配置一个集合视图以使用我自己的自定义单元格,但是,我总是想知道为什么我必须在 collectionview.register(任何类,forCellReuseIdentifier)内的单元格类中包含 .self 。
也许我的 OOP 技能在这里很缺乏,但是为什么 .self 会引用类型对象呢?为什么需要知道类型?任何帮助,将不胜感激。
func configureCollectionView() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: UICollectionViewLayout())
view.addSubview(collectionView)
collectionView.backgroundColor = .systemPink
//collectionView.register(FollowerCell.self, forCellWithReuseIdentifier: FollowerCell.reuseID)
collectionView.register(FollowerCell, forCellWithReuseIdentifier: FollowerCell.reuseID)
}
Run Code Online (Sandbox Code Playgroud)
class FollowerCell: UICollectionViewCell {
static let reuseID = "FollowerCell"
let avatarImageView = GFAvatarImageView(frame: .zero)
let userNameLabel = GFTitleLabel(textAlignment: .center, fontSize: 16)
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Run Code Online (Sandbox Code Playgroud)