在UICollectionViewCell中使用多个标识符

时间:2014-01-23 01:55:13

标签: objective-c uicollectionview uicollectionviewcell reuseidentifier

我想在UICollectionViewCell中使用多个标识符。

但似乎我只能为CollectionView设置一个重用标识符。

[collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];


它只使用一个标识符,但当我使用这样的不同标识符时,它会给出错误消息。

CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"NewID" forIndexPath:indexPath];
  

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'无法使视图出列   种类:带标识符的UICollectionElementKindCell   CollectionViewCell - 必须为标识符注册一个nib或类   或者在故事板中连接原型单元格

如何在UICollectionViewCell中设置多个标识符?

我想同时显示多个自定义单元格 每个单元格都有UIScrollView和UIPageControl 除非我可以设置不同的标识符,否则实例将重新用于新单元格,并且UIPageControl不会被每个UIScrollView中的移动所反应。

1 个答案:

答案 0 :(得分:1)

您必须为要使用的每个班级和单元格调用registerClass:forCellWithReuseIdentifier:

如果要使用具有不同重用标识符的单元格,则必须为它们创建不同的类,然后使用该重用标识符的集合视图注册这些类。


[collectionView registerClass:[FooCell class] 
    forCellWithReuseIdentifier:@"FooIdentifier"];

[collectionView registerClass:[BarCell class]     
    forCellWithReuseIdentifier:@"BarIdentifier"];

[collectionView registerClass:[ExampleCell class] 
    forCellWithReuseIdentifier:@"ExampleCell"];

现在,您可以使用具有这三个标识符中的任何一个的单元格。