【发布时间】:2018-07-08 13:30:00
【问题描述】:
我想用单元格填充我的收藏视图。所以我给它们设置了不同的尺寸,但它们应该彼此相邻。 我目前的情况如下:
我的 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,则无法完成大图右侧的两个图像。默认一个会将它们中的每一个并排放置(如果它们适合(以水平方式)),如果不合适,它将把它放在“下一行”中,不会再做任何事情。我在那里提供了样品:stackoverflow.com/questions/43186246/…
标签: ios swift uicollectionview