我是iOS和Swift的新手。我創建了一個集合視圖,它工作正常,但我想要在單個行中顯示多個項目。我試過,但沒有工作。有人能幫我嗎?提前致謝。Swift - UICollectionView連續顯示多個項目
這是我的代碼:
DashBoardCollectionVC
import UIKit
class DashBoardCollectionVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var main_collection_view: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = main_collection_view.dequeueReusableCell(withReuseIdentifier: "identify_collection_cell", for: indexPath) as! DashboardCollectionViewCell
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var collectionViewSize = collectionView.frame.size
collectionViewSize.width = collectionViewSize.width/2.0 //Display Three elements in a row.
collectionViewSize.height = collectionViewSize.height/4.0
return collectionViewSize
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Item Clicked : \(indexPath.item)")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}
它看起來像這樣
ContainerView設計
我要顯示在連續的兩個項目。我怎樣才能做到這一點?
看到這對e.g http://zappdesigntemplates.com/create-3-column-grid-view-with-uicollectionview/ –
您應該自定義UICollectionViewLayout – Lumialxk