【发布时间】:2013-12-06 10:33:31
【问题描述】:
在-collectionView:cellForItemAtIndexPath: 中,我正在向 UICollectionViewCell 添加自定义子视图,如下所示:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *const cellIdentifier = @"cellIdentifier";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
MyCustomViewClass *carouselView = [[MyCustomViewClass alloc] init];
[cell.contentView addSubview:carouselView];
return cell;
}
根据documentation、dequeueReusableCellWithReuseIdentifier:forIndexPath:“如果现有单元格可用,则将其出列或根据您之前注册的类或 nib 文件创建一个新单元格。”
问题是我对cellForItemAtIndexPath 的实现不断地创建MyCustomViewClass 的新实例。即使后者的实例在它们离开屏幕时从集合视图中删除,但每次调用 dequeueReusableCellWithReuseIdentifier:forIndexPath: 时创建一个新实例似乎仍然是错误的。
我的问题是,鉴于 MyCustomViewClass 实例是图形密集型的并且占用内存,那么延迟加载它们的最佳方法是什么?我必须实现自己的队列吗?还是应该让它成为 UICollectionViewCell 的子类?
【问题讨论】:
标签: ios uicollectionview uicollectionviewcell