我是否需要从数据源的 cellForPath: 中调用 UICollectionView 的 dequeueCell:? [英] Am I required to call UICollectionView's dequeueCell: from within the data source's cellForPath:?

查看:13
本文介绍了我是否需要从数据源的 cellForPath: 中调用 UICollectionView 的 dequeueCell:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的非常想为我的 UICollectionView 提供静态"单元格,就像 UITableView 的过去一样.但我知道,我们好孩子们必须使用 dequeueReusableCellWithReuseIdentifier:forIndexPath: 作为我们细胞的工厂.因此,我提出了以下方案,并要求对其潜力进行反馈.到目前为止,它对我有用,但我害怕一个未知的陷阱.

I really, really want to provide 'static' cells to my UICollectionView, ala the old days with UITableView. But I know that we good boys and girls must use dequeueReusableCellWithReuseIdentifier:forIndexPath: as a factory for our cells. So I propose the following scheme, and request feedback on its potential. So far, it is working for me, but I dread an unknown gotcha.

#import "MyCellClass.h"

@implementation MyViewController {
   MyCellClass *_cell0; // etc - many are possible. could store in array
}

-(void)viewDidLoad {
   // assume MyCellClass has a +nib and +reuseId. The reader understands.
   [_collectionView registerNib:[MyCellClass nib] forCellWithReuseIdentifier:[MyCellClass reuseId]];
}

-(void)viewDidAppear:animated {
   // this is where the fun begins. assume proper counts coming from the datasource
   NSIndexPath *indexPath = [NSIndexPath indexPathWithRow:0 inSection:0];
   _cell0 = [[self collectionView] dequeueReusableCellWithReuseIdentifier:[MyCellClass reuseId] forIndexPath:indexPath];
   // etc
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   if ([indexPath row] == 0) return _cell0;
   // etc
}

我故意掩盖细节,例如配置单元格.疯狂?天才?只是现状?到目前为止它似乎正在工作,但我担心,不知何故,Apple 期望从 cellForPath 中调用 dequeue.有什么想法吗?

I deliberately gloss over details such as configuring the cell. Madness? Genius? Just status quo? It seems to be working so far, but I fear that, somehow, Apple expects dequeue to be called from within cellForPath. Any thoughts?

推荐答案

Apple TSI Guy 的回应:

虽然您说您的应用可以运行,但您确实应该使用数据源中的dequeueReusableCellWithReuseIdentifier"方法cellForItemAtIndexPath".那是支持的使用模式.不要试图抓住你的 MyCellClass,让集合视图管理如果需要,它会要求您将其出列.

Although you said your app works, you really should use "dequeueReusableCellWithReuseIdentifier" from within the data source method "cellForItemAtIndexPath". That is the supported use pattern. Don't try to hold on you MyCellClass, let the collection view manage that and it will ask you to dequeue it if necessary.

我会有点撅嘴,更改我的代码,然后发出增强请求.但我告诉你它工作得很好!

I'm going to pout a little, change my code, and then issue an enhancement request. But I tell you it worked just fine!

这篇关于我是否需要从数据源的 cellForPath: 中调用 UICollectionView 的 dequeueCell:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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