Qma*_*485 0 ios uicollectionview uicollectionviewcell swift
我正在配置一个集合视图以使用我自己的自定义单元格,但是,我总是想知道为什么我必须在 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)
这就是 Swift 的工作方式。当要求您传递类型作为参数时,您不能传递类型的裸名称;你通过名字加上.self.
你只需要接受这就是语言的工作原理。就像为什么法语中的“yes”是“oui”;你不要问为什么,你只需学习它并使用它。或者为什么板球比赛中边界击球为 4 分?你不要问为什么,这只是游戏规则。
| 归档时间: |
|
| 查看次数: |
460 次 |
| 最近记录: |