我試圖做所有其他問題似乎都做的事情,但仍然出現錯誤。也許我錯過了一些明顯的東西,但我查看了文檔,看起來你需要一個佈局來初始化。即使在初始化ViewDidLoad後,UICollectionView也必須初始化爲非零布局參數
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout)
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.register(InterestCollectionViewCell.self, forCellWithReuseIdentifier: interestReuseIdentifier)
view.addSubview(collectionView!)
集合視圖的委託方法
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return interests.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let vc = collectionView.dequeueReusableCell(withReuseIdentifier: interestReuseIdentifier, for: indexPath) as! InterestCollectionViewCell
vc.interestLabel.text = interests[indexPath.row].rawValue as String
return vc
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.size.width, height: 45)
}
顯示你的collectionview代表方法 –
看來你有代表問題。使用此UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout – Joe
更新您的委託您從代碼中缺少佈局項目大小.... layout.itemSize = CGSize(寬度:100,高度:100) – Joe