在 UICollectionViewCell 上设置 .reuseIdentifier [英] Setting .reuseIdentifier on a UICollectionViewCell

查看:24
本文介绍了在 UICollectionViewCell 上设置 .reuseIdentifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的 UICollectionViewCell 我想实例化自己,并添加到 UICollectionView.为了使其工作,UICollectionViewCell 实例需要设置其 .reuseIdentifier 属性.

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.

通常,描述单元格的类或 Nib 会在集合视图中注册,并且集合视图会使用已设置的 .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:

- registerClass:forCellWithReuseIdentifier:
- registerNib:forCellWithReuseIdentifier:

但是,由于我是在集合视图之外构建此单元格,因此这些不适用.

However, since I am constructing this cell outside of the collection view, these do not apply.

当我自己创建单元格时,似乎无法设置它的 .reuseIdentifier (因为它是只读属性,并且没有 init... 初始化它的方法).

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没有设置时,UICollectionView在添加单元格时会抛出异常.此行为不同于 UITableView,其中重用标识符是可选的.

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.

设置集合视图单元格的重用标识符的简单解决方法是将其嵌入到 .xib 文件中并使用 Identifier 框,然后创建单元格的实例与

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

[NSBundle.mainBundle loadNibNamed:@"MyCellName" owner:self options:nil][0];

然后我可以通过上面实例化的 UICollectionViewCell 并且一切正常.

I can then pass the above-instantiated UICollectionViewCell and everything works fine.

...但这似乎是一个非常愚蠢和任意的箍跳.是否有其他方法可以在没有 .xib-wrapper 绕道的情况下在单元实例上设置此属性?

...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?

更新:Apple 的文档是这样说的:

Update: Apple's documentation says this:

为简化代码的创建过程,集合视图要求您始终使视图出队,而不是在代码中显式创建它们.

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.

...这实际上是不正确的,因为它不需要这个(即,只要设置了标识符以某种方式,外部实例单元就可以正常工作,例如,如果从 .xib),并且在我的特定用例中它也没有简化我的代码的创建过程"(而是需要一个额外的文件;此外,要求集合视图创建这些文件会很麻烦一些复杂的一次性).

...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).

但上述内容似乎暗示这个问题的答案是否定的:故意很难"创建一个可用的单元格,除非让集合视图将其出列.

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.

推荐答案

我很抱歉这么说,但我认为你将不得不接受这样一个事实,即 UICollectionView 是生产 UICollectionViewCells 的唯一 Apple 品牌工厂.因此,如果您有两个集合视图要在其中显示完全相同的 object,那么您就不走运了!

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!

好消息是,如果您有两个集合视图,您希望在其中显示一个 看起来 与另一个完全相同的单元格,那么您可以完成您的任务.很简单:

The good news is that if you have two collection view in which you want to display a cell that looks exactly 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

同时,无论哪个类拥有 UICollectionView cv1 和 cv2

Meanwhile, in whichever classes own the UICollectionView cv1 and cv2

[[self cv1] registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];

[[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;
}

这篇关于在 UICollectionViewCell 上设置 .reuseIdentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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