gpt4 book ai didi

ios - Swift - 如何用单元格填充 Collection View

转载 作者:行者123 更新时间:2023-11-28 12:11:06 25 4
gpt4 key购买 nike

我想用单元格填充我的 Collection View 。所以我调整了它们的大小,但它们应该并排放置。我目前的情况如下: My current situation

我希望单元格彼此相邻。如下: Goal situation

我的 Collectionview-Class 如下所示:

class OverviewViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
super.viewDidLoad()
setupCollection()
setupView()
}

private func setupCollection() {
self.collectionView!.register(GoalCell.self, forCellWithReuseIdentifier: goalCellIdentifier)
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
self.collectionView?.backgroundColor = .white
}

private func setupView() {
self.navigationItem.title = "Goals"
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return DataProvider.sharedInstance.getGoals().count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: goalCellIdentifier, for: indexPath) as! GoalCell
cell.goalEntry = DataProvider.sharedInstance.getGoals()[indexPath.row]
cell.setup()
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellHeight = UIScreen.main.bounds.height / 4.0 - 20
let cellWidth = UIScreen.main.bounds.width / 3.0 - 1.0
let cell = DataProvider.sharedInstance.getGoals()[indexPath.row]
switch cell.type! {
case GoalType.daily:
return CGSize(width: cellWidth, height: cellHeight)
case GoalType.yearly:
return CGSize(width: cellWidth * 2, height: cellHeight * 2)
case GoalType.lifely:
return CGSize(width: cellWidth * 3, height: cellHeight * 3)
}
}

我使用以下代码初始化了我的 collectionview:

let overviewFlowLayout = UICollectionViewFlowLayout()
overviewFlowLayout.minimumLineSpacing = 0.0
overviewFlowLayout.minimumInteritemSpacing = 0.0
let overviewVC = OverviewNavViewController(rootViewController: OverviewViewController(collectionViewLayout: overviewFlowLayout))

我真的不知道该怎么办了...希望有人能帮帮我!谢谢!

最佳答案

您正在使用 UICollectionViewFlowLayout,它无法按照您的想法工作。要获得第二个屏幕截图中显示的布局类型,您必须编写自己的布局类。

关于ios - Swift - 如何用单元格填充 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com