xcode 在 UICollectionViewCell 上设置 .reuseIdentifier
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13281631/
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
Setting .reuseIdentifier on a UICollectionViewCell
提问by mjh
I have a particular UICollectionViewCell
that I want to instantiate myself, and add to a UICollectionView
. In order for this to work, the UICollectionViewCell
instance needs its .reuseIdentifier
property to be set.
我有一个特别UICollectionViewCell
想实例化自己,并添加到UICollectionView
. 为了使其工作,UICollectionViewCell
实例需要.reuseIdentifier
设置其属性。
Normally, the class or Nib that describes the cell is registered with the collection view, and the collection view instantiates the cell with its .reuseIdentifier
already set, with these methods:
通常,描述单元格的类或 Nib 会在集合视图中注册,并且集合视图会使用.reuseIdentifier
已设置的单元格实例化单元格,使用以下方法:
- registerClass:forCellWithReuseIdentifier:
- registerNib:forCellWithReuseIdentifier:
However, since I am constructing this cell outside of the collection view, these do not apply.
但是,由于我是在集合视图之外构建此单元格,因此这些不适用。
When I create the cell myself, there appears to be no way to set its .reuseIdentifier
(because it is a readonly property, and there are no init...
methods that initialize it).
当我自己创建单元格时,似乎无法设置它.reuseIdentifier
(因为它是只读属性,并且没有init...
初始化它的方法)。
When .reuseIdentifier
is not set, the UICollectionView
throws an exception when the cell is added. This behavior is different from UITableView
, where reuse identifiers were optional.
如果.reuseIdentifier
未设置,则UICollectionView
在添加单元格时抛出异常。此行为与 不同UITableView
,其中重用标识符是可选的。
An easy workaround to set the collection view cell's reuse identifier is to embed it in a .xib
file and use the Identifier
box, then create an instance of the cell with
设置集合视图单元格的重用标识符的一个简单解决方法是将其嵌入到.xib
文件中并使用该Identifier
框,然后使用以下命令创建单元格的实例
[NSBundle.mainBundle loadNibNamed:@"MyCellName" owner:self options:nil][0];
I can then pass the above-instantiated UICollectionViewCell
and everything works fine.
然后我可以通过上面的实例化UICollectionViewCell
并且一切正常。
...but that seems like a pretty silly and arbitrary hoop to jump through. Is there some other way to get this property set on the cell instance without the .xib
-wrapper detour?
......但这似乎是一个非常愚蠢和随意的跳跃。有没有其他方法可以在没有.xib
-wrapper detour 的情况下在单元实例上设置此属性?
Update: Apple's documentation says this:
更新:Apple 的文档是这样说的:
To simplify the creation process for your code, the collection view requires that you always dequeue views, rather than create them explicitly in your code.
为了简化代码的创建过程,集合视图要求您始终将视图出列,而不是在代码中显式创建它们。
...which is actually not true, because it doesn't require this (i.e., externally-instanced cells work fine as long as their identifier is set somehow, e.g. if loaded from a .xib
), and it also doesn't "simplify the creation process for my code" in my particular use case (rather requires an extra file; further it would be messy to require that the collection view create these few complex one-offs).
...这实际上不是真的,因为它不需要这个(即,只要它们的标识符以某种方式设置,外部实例化的单元就可以正常工作,例如,如果从 a 加载.xib
),并且它也不会“简化我的代码的创建过程”在我的特定用例中(而需要一个额外的文件;此外,要求集合视图创建这几个复杂的一次性文件会很麻烦)。
But the above does seem to imply that the answer to the question is no: it's intentionally "difficult" to create a usable cell except by having the collection view dequeue it.
但以上似乎暗示问题的答案是否定的:除非让集合视图将其出列,否则故意“难以”创建可用的单元格。
回答by QED
I'm sorry to say it, but I think you're going to have to accept the fact that UICollectionView is the sole Apple-branded factory for producing UICollectionViewCells. So if you have two collection views in which you want to display the exact same object, you're out of luck!
我很抱歉这么说,但我认为您将不得不接受这样一个事实,即 UICollectionView 是唯一一家生产 UICollectionViewCells 的 Apple 品牌工厂。因此,如果您有两个集合视图要在其中显示完全相同的对象,那么您就不走运了!
The good news is that if you have two collection view in which you want to display a cell that looksexactly the same as in the other, then you can accomplish your task. It's easy:
好消息是,如果你有两个集合视图中要显示的单元格的外观完全一样,在对方,那么你就可以完成你的任务。这很简单:
@interface MyModelClass : NSObject
// a bunch of data
@end
@interface MyCollectionViewCell : UICollectionViewCell
-(void)refreshCellWithObject:(MyModelClass *)object;
@end
Meanwhile, in whichever classes own the UICollectionView cv1 and cv2
同时,在拥有 UICollectionView cv1 和 cv2 的任何类中
[[self cv1] registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
and
和
[[self cv2] registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
And their data sources can look the same
并且他们的数据源看起来是一样的
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyModelObject *obj = [self theObjectIWant];
MyCollectionViewCell *cell;
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
[cell refreshCellWithObject:obj];
return cell;
}
回答by sunkehappy
Use these two methods.
使用这两种方法。
– registerNib:forCellWithReuseIdentifier:
– dequeueReusableCellWithReuseIdentifier:forIndexPath:
See apple's UICollectionView Class Reference
参见苹果的UICollectionView 类参考