xcode 使用 Interface Builder 在 nib 中设计 UICollectionView 单元格(没有故事板)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22056153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Designing UICollectionView cells in nib with Interface Builder (without storyboard)
提问by Can Poyrazo?lu
I am trying to design a custom UICollectionViewCell
prototype (in Xcode 5.0.2), however Interface Builder doesn't let me add a cell to my UICollectionView
while designing a nib. I can set the number of items (cells) and Interface Builder creates and displays cells perfectly if I'm using storyboard, but I can't add a cell to my collection view in a nib. I've tried:
我正在尝试设计一个自定义UICollectionViewCell
原型(在 Xcode 5.0.2 中),但是 Interface Builder 不允许我UICollectionView
在设计笔尖时向我添加单元格。如果我使用故事板,我可以设置项目(单元格)的数量,并且 Interface Builder 可以完美地创建和显示单元格,但是我无法在笔尖中将单元格添加到我的集合视图中。我试过了:
- Drag and dropping collection view cell into collection view manually from the object library. (fails: doesn't let me drop the cell anywhere in my view)
- Creating my collection view with cells in storyboard and copy-pasting the whole view into nib. (fails: collection view is copied but the cell is gone)
- Creating my collection view with cells in storyboard, opening the storyboard as source code, finding my collection view cells, copying the relevant XML, opening my nib as source code, pasting it inside my collection view in XML. (fails: unable to open the nib in Interface Builder, it gives errors. When I remove the cell from source code, it opens again. Do nottry this if you don't know what you are doing.)
- 从对象库中手动将集合视图单元格拖放到集合视图中。(失败:不允许我将单元格放在视野中的任何位置)
- 使用情节提要中的单元格创建我的集合视图并将整个视图复制粘贴到 nib 中。(失败:集合视图被复制但单元格不见了)
- 使用情节提要中的单元格创建我的集合视图,将情节提要作为源代码打开,找到我的集合视图单元格,复制相关的 XML,将我的笔尖作为源代码打开,将其粘贴到 XML 格式的集合视图中。(失败:无法打开在Interface Builder笔尖,它给了错误,当我删除源代码中的细胞,它再次打开待办事项。不试试这个,如果你不知道自己在做什么。)
I've also seen several questions about the same issue:
我还看到了几个关于同一问题的问题:
- Is it possible to create prototype cells in Interface Builder without story boards?
- Custom Header in UICollectionView with Interface Builder without Storyboard
- Prototype Cells in a nib instead of a storyboard
They all point out to doing them programatically and/or using another nib for the cell. I know how to do them, but is there any way to design the collection view cell, inside a collection view inside the same viewin a nib, just as in storyboard? Why doesn't Interface Builder let me do that in nib where it allows (and even encourages) perfectly using storyboard?
他们都指出以编程方式进行操作和/或为单元使用另一个笔尖。我知道该怎么做,但是有什么方法可以像在故事板中一样在笔尖的同一视图内的集合视图中设计集合视图单元格?为什么 Interface Builder 不让我在 nib 中做到这一点,它允许(甚至鼓励)完美地使用故事板?
采纳答案by Daniel Galasko
The simple answer is no, this cannot be done. Think of a storyboard as a collection of XIBs. A XIB simply defines the facets of a particular view so that it can be constructed at runtime.
简单的答案是否定的,这是不可能的。将故事板视为 XIB 的集合。XIB 只是定义特定视图的方面,以便它可以在运行时构建。
Regarding collection views and their storyboard implementations, it's important to understand that a storyboard allows for nesting of viewcontrollers and defining collection views with their XIBs because that keeps the fundamental paradigm of storyboards coherent. Since a storyboard is the means of defining the "story" or scene of an application it is only natural that it allows for the declaration of the reusable views for use inside a collection view.
关于集合视图及其故事板实现,重要的是要了解故事板允许嵌套视图控制器并使用它们的 XIB 定义集合视图,因为这可以保持故事板的基本范式连贯。由于故事板是定义应用程序“故事”或场景的手段,因此它允许声明可重用视图以在集合视图中使用是很自然的。
The same cannot be said for XIBs because the fundamental idea behind XIBs is in reusability. This will allow a collection view defined in a XIB to have any cells used with it as long as the controller registers these classes with the collection view. This way you get the benefit of reusability as another controller can use the same XIB and register different cells etc.
对于 XIB 则不能这样说,因为 XIB 背后的基本思想是可重用性。这将允许在 XIB 中定义的集合视图使用任何单元格,只要控制器向集合视图注册这些类。通过这种方式,您可以获得可重用性的好处,因为另一个控制器可以使用相同的 XIB 并注册不同的单元格等。
So I think it would be far more confusing to allow for the declaration of the supported cells of a collection view inside a XIB since that breaks the single responsibility principle(if it can be called that) that XIBs aspire to.
所以我认为允许在 XIB 中声明集合视图的支持单元格会更加混乱,因为这打破了 XIB 所追求的单一责任原则(如果可以这样称呼的话)。
Your best solution would be to define a custom collection view subclass that registers the relevant cells on instantiation, and then use this class in your XIB.
您最好的解决方案是定义一个自定义集合视图子类,该子类在实例化时注册相关单元格,然后在您的 XIB 中使用此类。