使用Interface Builder在没有故事板的情况下在nib中设计UICollectionView单元格 [英] Designing UICollectionView cells in nib with Interface Builder (without storyboard)

查看:71
本文介绍了使用Interface Builder在没有故事板的情况下在nib中设计UICollectionView单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个自定义 UICollectionViewCell 原型(在Xcode 5.0.2中),但是Interface Builder不允许我向我的<$ c $添加一个单元格c>设计nib时的UICollectionView 。如果我正在使用storyboard,我可以设置项目(单元格)和Interface Builder创建的数量并完美显示单元格,但我无法在笔尖中将单元格添加到我的集合视图中。我试过了:

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:


  • 从对象库中手动将集合视图单元拖放到集合视图中。 (失败:不允许我在我的视图中将单元格放在任何位置)

  • 使用storyboard中的单元格创建我的集合视图,并将整个视图复制粘贴到nib中。 (失败:复制集合视图但单元格已消失)

  • 使用storyboard中的单元格创建我的集合视图,打开storyboard作为源代码,查找我的集合视图单元格,复制相关的XML ,打开我的nib作为源代码,将其粘贴到我的XML集合视图中。 (失败:无法在Interface Builder中打开nib,它会出错。当我从源代码中删除单元格时,它会再次打开。如果你不知道你在做什么,请试试这个。)

  • 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 not try this if you don't know what you are doing.)

我也看到了几个关于同一问题的问题:

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

他们都指出要做programati cally和/或使用另一个笔尖为细胞。我知道如何做到这一点,但有没有办法设计集合视图单元格,在一个集合视图里面的同一个视图,就像在故事板中一样?为什么Interface Builder不允许我在nib中允许(甚至鼓励)完美地使用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 view in 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?

推荐答案

简单的答案是否定的,这是不可能做到的。将故事板视为XIB的集合。 XIB只是定义了特定视图的方面,以便可以在运行时构建它。

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.

关于集合视图及其故事板实现,了解故事板允许这一点非常重要用于嵌套viewcontrollers并使用它们的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而言,这是不可能的,因为XIB背后的基本思想是可重用性。只要控制器使用集合视图注册这些类,这将允许在XIB中定义的集合视图具有与其一起使用的任何单元。通过这种方式,您可以获得可重用性的好处,因为另一个控制器可以使用相同的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所追求的单一责任原则(如果可以调用它)。

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中使用这个类。

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.

这篇关于使用Interface Builder在没有故事板的情况下在nib中设计UICollectionView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆