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

查看:134
本文介绍了我是否需要从数据源的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的回复:


虽然你说你的应用程序有效,但你真的应该在数据源
方法cellForItemAtIndexPath中使用
dequeueReusableCellWithReuseIdentifier。这是受支持的使用模式。
不要试图抓住你的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天全站免登陆