我是 iOS 和 Swift 的新手。我创建了一个集合视图,它工作正常,但我想在一行中显示多个项目。我试过但它不起作用。有人能帮我吗?提前致谢。
这是我的代码:
仪表板集合VC
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?) {
}
}
看起来像这样
容器视图设计
我想连续显示两个项目。我怎样才能做到这一点?