0
我有一個集合視圖。我想讓每個單元格以編程方式成行。這是我迄今爲止所做的:收集單元格中的某一行的單元格很快?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.challenges.count
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
cell.myLabel.text = self.challenges[indexPath.item]
cell.backgroundColor = .orange
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
}
}
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
}
我無法找到您提到的功能。我試圖按照如下方式編寫它:但我根本不會被調用: func collectionView(_ collectionView:UICollectionView,layout collectionViewViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:IndexPath) - > CGSize {size = CGSize(width:UIScreen .main.bounds.width,height:10) return size; } –
viewcontroller是否包含實現UICollectionViewDelegateFlowLayout協議的函數?如果是這樣,你是否將它設置爲collectionView的代表? – Houwert