简体   繁体   English

Swift UI集合视图布局

[英]Swift UI collection view layout

I am trying to use a UIcollectionView in my SpriteKit game to display the level select scene. 我正在尝试在SpriteKit游戏中使用UIcollectionView来显示级别选择场景。 The collection view has 6 sections (start, world 1-5) with 8 cells in each (level 1-8). 集合视图有6个部分(开始,世界1-5),每个部分有8个单元(1-8级)。 They are custom cells with a simple UILabel for now. 它们是带有简单UILabel的自定义单元格。 I can use the did select item for index path to load my levels, it's all good. 我可以使用did select项目作为索引路径来加载我的关卡,这一切都很好。 However my problem is the following 但是我的问题是以下

The first section in the collection view is the start screen so I don't want the collection view to show the 8 cells in that section. 集合视图的第一部分是“开始”屏幕,因此我不希望集合视图显示该部分中的8个单元格。 That section is supposed o only showing a background image and a tap to start lable. 假定该部分仅显示背景图像和水龙头以启动标签。 So what I tried in cellForIndexPath is to 所以我在cellForIndexPath中尝试的是

1) hide cells in that section but that causes text label issues with the cells in the other sections 1)隐藏该部分中的单元格,但这会导致其他部分中的单元格出现文本标签问题

2) hide the text labels and make the cells color transparent, same problem as 1 2)隐藏文本标签并使单元格颜色透明,与1相同的问题

So basically what could I do to solve this issue? 那么基本上我该怎么做才能解决这个问题?

I could put the start section into a different SKScene but I prefer if it's all in the collection view. 我可以将开始部分放到另一个SKScene中,但我更喜欢将其全部放在集合视图中。 Another option I was thinking is to make each section have only 1 cell, the size of screen, and add 8 UIButtons to each section except the start section. 我在想的另一个选择是使每个部分只有一个单元格(屏幕大小),并为每个部分(开始部分除外)添加8个UIButton。 I also need some of those cells/buttons to be disabled until the previous level is unlocked. 我还需要禁用其中一些单元格/按钮,直到解锁上一个级别。 I am not sure what a better approach is, 8 cells as buttons or 8 UIButtons in 1 cell. 我不确定哪种更好的方法是8个单元格作为按钮或8个UIButtons在1个单元格中。

I am only looking for a UICollectionView solution, I already have an alternative, because it will make my life much easier when converting to tvOS and using the focus stuff and navigating through 6 sections with 30+ buttons 我只是在寻找UICollectionView解决方案,我已经有了另一种选择,因为当转换为tvOS并使用焦点内容并使用30多个按钮浏览6个部分时,它将使我的生活更加轻松

Thank you very much for any help 非常感谢您的帮助

I'd go for 8 cells as buttons rather than using 1 cell with a series of UIButtons, that will keep things simple and allow you to use didSelectItemAtIndexPath. 我将使用8个单元格作为按钮,而不是将1个单元格与一系列UIButton一起使用,这将使事情保持简单,并允许您使用didSelectItemAtIndexPath。 Can't you just use numberOfItemsInSection to define only 1 cell in the first section? 您不能只使用numberOfItemsInSection在第一部分中仅定义一个单元格吗?

My approach would probably be to use a custom flow layout and a supplementary view for your background image and tap to start button - a bit complex but very flexible. 我的方法可能是对背景图像使用自定义流程布局和辅助视图,然后点击开始按钮-有点复杂但非常灵活。

You can register more that one kind of cell in UICollectionView: 您可以在UICollectionView中注册一种以上的单元格:

  • If you are using Interface builder add another Collection View Cell from Objects library and set it's reuse identifier 如果您正在使用“界面”构建器,请从“对象”库中添加另一个“收集视图”单元并设置其重用标识符
  • Otherwise register nib or class in viewDidLoad method of your UICollectionViewController using self.collectionView?.registerNib(forCellWithReuseIdentifier:) or self.collectionView?.registerClass(forCellWithReuseIdentifier:) 否则,使用self.collectionView?.registerNib(forCellWithReuseIdentifier:)self.collectionView?.registerClass(forCellWithReuseIdentifier:)在UICollectionViewController的viewDidLoad方法中注册笔尖或类。

Then when you dequeue cell use proper reuse identifier: 然后,当您使单元出队时,请使用适当的重用标识符:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var reuseIdentifier = "identifier of cell with 8 button"
    if indexPath.section == 0 {
        reuseIdentifier = "identifier of start cell as you set in IB of viewDidLoad"
    }
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

    // Configure the cell depending on indexPath

    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM