Ios 延迟加载UICollectionViewCell的自定义子视图

Ios 延迟加载UICollectionViewCell的自定义子视图,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,在-collectionView:cellForItemAtIndexPath:中,我将向UICollectionViewCell添加自定义子视图,如下所示: -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *const cellIdentifier = @"cellId

-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;
}
根据,
dequeueReusableCellWithReuseIdentifier:forIndexPath:
“如果现有单元格可用,则将其退出队列,或者基于您以前注册的类或nib文件创建一个新的单元格。”

问题是我的
cellForItemAtIndexPath
实现不断创建
MyCustomViewClass
的新实例。尽管后者的实例不在屏幕上时会从集合视图中删除,但每次调用
dequeueReusableCellWithReuseIdentifier:forIndexPath:
时创建一个新实例似乎仍然是错误的


我的问题是,考虑到
MyCustomViewClass
实例是图形密集型的,并且占用内存,那么延迟加载它们的最佳方式是什么?我必须实现自己的队列吗?或者我应该将其作为UICollectionViewCell的子类吗?

因为您这样做了
MyCustomViewClass*CarouseView=[[MyCustomViewClass alloc]init]
,每次系统调用
dequeueReusableCellWithReuseIdentifier:forIndexPath:
时,它都会为您创建一个新的
MyCustomViewClass
实例。因此,您需要做的是检查
MyCustomViewClass
的实例是否已添加到该单元格中,如果没有,则创建一个新实例。

因为您这样做了
MyCustomViewClass*carouselView=[[MyCustomViewClass alloc]init]
,每次系统调用
dequeueReusableCellWithReuseIdentifier:forIndexPath:
时,它都会为您创建一个新的
MyCustomViewClass
实例。因此,您需要做的是检查
MyCustomViewClass
的实例是否已添加到该单元格中,如果没有,则创建一个新实例。

因为您这样做了
MyCustomViewClass*carouselView=[[MyCustomViewClass alloc]init]
,每次系统调用
dequeueReusableCellWithReuseIdentifier:forIndexPath:
时,它都会为您创建一个新的
MyCustomViewClass
实例。因此,您需要做的是检查
MyCustomViewClass
的实例是否已添加到该单元格中,如果没有,则创建一个新实例。

因为您这样做了
MyCustomViewClass*carouselView=[[MyCustomViewClass alloc]init]
,每次系统调用
dequeueReusableCellWithReuseIdentifier:forIndexPath:
时,它都会为您创建一个新的
MyCustomViewClass
实例。因此,您需要做的是检查是否已将
MyCustomViewClass
的实例添加到该单元格中,如果未添加,则创建一个新实例。

我不知道MyCustomViewClass的功能,但您可以创建一个已关联该CustomViewClass的自定义UICollectionViewCell

如果您的CustomViewClass扩展了UIView,则更简单。如果你使用的是故事板,那就更简单了。在故事板中,您不需要为此创建自定义UICollectionViewCell。您可以将UIVIew拖动到CollectionViewCell,并将customView设置为MyCustomViewClass。这样,它将只创建一次,然后将被重用


如果MyCustomViewClass具有某种状态(假设这是一个带有百分比的状态栏)您可以重置该状态您必须扩展UICollectionViewCell并覆盖
prepareforeuse

我不知道MyCustomViewClass的功能,但您可以创建一个自定义UICollectionViewCell,该自定义UICollectionViewCell已经与该CustomViewClass关联

如果您的CustomViewClass扩展了UIView,则更简单。如果你使用的是故事板,那就更简单了。在故事板中,您不需要为此创建自定义UICollectionViewCell。您可以将UIVIew拖动到CollectionViewCell,并将customView设置为MyCustomViewClass。这样,它将只创建一次,然后将被重用


如果MyCustomViewClass具有某种状态(假设这是一个带有百分比的状态栏)您可以重置该状态您必须扩展UICollectionViewCell并覆盖
prepareforeuse

我不知道MyCustomViewClass的功能,但您可以创建一个自定义UICollectionViewCell,该自定义UICollectionViewCell已经与该CustomViewClass关联

如果您的CustomViewClass扩展了UIView,则更简单。如果你使用的是故事板,那就更简单了。在故事板中,您不需要为此创建自定义UICollectionViewCell。您可以将UIVIew拖动到CollectionViewCell,并将customView设置为MyCustomViewClass。这样,它将只创建一次,然后将被重用


如果MyCustomViewClass具有某种状态(假设这是一个带有百分比的状态栏)您可以重置该状态您必须扩展UICollectionViewCell并覆盖
prepareforeuse

我不知道MyCustomViewClass的功能,但您可以创建一个自定义UICollectionViewCell,该自定义UICollectionViewCell已经与该CustomViewClass关联

如果您的CustomViewClass扩展了UIView,则更简单。如果你使用的是故事板,那就更简单了。在故事板中,您不需要为此创建自定义UICollectionViewCell。您可以将UIVIew拖动到CollectionViewCell,并将customView设置为MyCustomViewClass。这样,它将只创建一次,然后将被重用


如果您的MyCustomViewClass具有某种状态(假设是一个带有百分比的状态栏),则可以重置该状态。您必须扩展UICollectionViewCell并覆盖
prepareForReuse

谢谢,我尝试过,但没有成功。从
dequeueReusableCellWithReuseIdentifier
返回的所有单元格都有一个空的
subviews
数组(即
cell.contentView.subviews
总是空的)。谢谢,我试过了,但是