我試圖編輯這個單元格的寬度,這樣無論屏幕大小如何,我都會在一行中有score1.count
個單元格。我用於收集視圖的代碼如下:編輯一個CollectionViewCell的寬度
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
cell.myLabel.text = String(self.items[indexPath.item])
return cell
}
這是針對單元格寬度的函數。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = Int(self.view.frame.size.width)/(score1.count + 1)
return CGSize(width: CGFloat(size), height: 80)
}
我花了很多時間看着不同的來源和嘗試不同的事情,但收集視圖從來沒有正確數量的單元格。一個想法是,它可能是因爲我有自動佈局設置單元格大小,但每當我去改變它xcode崩潰。任何想法繞過自動佈局?或者如何改變自動佈局?
謝謝!
你設置'UICollectionViewFlowLayout'? –
不,但我沒有在代碼中引用它。 –
將UICollectionViewDelegate更改爲UICollectionViewDelegateFlowLayout並且它工作正常!多謝你們! –