Ios UICollectionView和UITextView的速度非常慢

Ios UICollectionView和UITextView的速度非常慢,ios,uicollectionview,Ios,Uicollectionview,我有一个UICollectionView,带有自定义UICollectionViewCell。 每个单元格内部都有一个UITextView。 问题:UICollectionView非常慢,在模拟器中也是如此 -(void)viewDidLoad { [self.collectionView registerClass:[AgendaCollectionCell class] forCellWithReuseIdentifier:agendaCellIdentifier]; }

我有一个UICollectionView,带有自定义UICollectionViewCell。 每个单元格内部都有一个UITextView。 问题:UICollectionView非常慢,在模拟器中也是如此

-(void)viewDidLoad
{
[self.collectionView registerClass:[AgendaCollectionCell class]
         forCellWithReuseIdentifier:agendaCellIdentifier];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


 AgendaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:agendaCellIdentifier forIndexPath:indexPath];

cell.delegate = self;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
[cell setCurrentDateAtIndex:([indexPath row]) date:currentMonth events:events];


// Return the cell
return cell;
}

我检查了档案器

每个单元格都进行与日期相关的计算。 如您所见,loadNibNamed需要花费太多的时间来加载。此外,UITextView占用的空间太大。 我在这里搜索了很多问题,并使用了他们的一些答案(我还缓存了NSCalendar的所有实例)。我不明白为什么加载大约需要435毫秒。UICollectionView有40个单元格(包含一个月的天数)。 我是否应该放弃使用UICollectionView并通过使用drawRect转换为自定义视图

编辑 我认为答案中建议了一个好的观点:使用UNib而不是registerClass加载单元。我可以看到一个非常大的性能提升:从400毫秒到98毫秒

试试推杆

UINib * nib = [UINib nibWithNibName:@"YourNibName" bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"MyCell"];
视图中加载
,然后不要自己创建单元格(在

实现总是调用

UICollectionViewCell * cell = [collectionView dequeueCell...]

这样,我认为,
collection视图将只加载nib一次(在您的
viewDidLoad
方法中,然后在需要时创建它的副本。

实际上,在我的viewDidLoad中,我有[self.collectionView registerClass:[AgendaCollectionCell类]forCellWithReuseIdentifier:agendacellidentier]我看到你正在加载NIB在 ItIsFrase< /Cord>中。我自己不会因为这个原因而精确地做这件事。例如,你不能控制加载到<代码> CoputsVIEW/COD>。也许考虑转过来,注册NIB,将自定义类放入NIB并加载I。我想你指定的初始化器应该是
initWithCoder:
,而不是
initWithFrame
。我不明白,抱歉。我编辑了代码。你能解释一下你的评论吗?哦,我明白了。没关系。我仍然建议尝试注册nib(加载nib一次)而不是注册类,从而加载nib x次,x是同时可见的单元格数+~3。对不起,我应该在哪里注册自定义单元格?它是在类的viewDidLoad中注册的,我使用cellForItem。。。
UICollectionViewCell * cell = [collectionView dequeueCell...]