我有这个代码 让cellId =“cellId”
class FeedController: UICollectionViewController,UICollectionViewDelegateFlowLayout{
let lb: UILabel = {
let lb = UILabel()
lb.text="sasa"
return lb
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(lb);
navigationItem.title = "Centill"
collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
}
class FeedCell: UICollectionViewCell {
override init(frame: CGRect){
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = UIColor.yellow
}
}
但是这段代码不起作用,我尝试添加realoadData
,但它既不起作用,可能是什么问题?我没有使用故事板。