Ios7 ios 7中的-[UICollectionView\u dequeueReusableViewOfKind:with Identifier:forIndexPath:viewCategory:]中的断言失败
我在使用两个UICollectionView时遇到此错误。 我搜索这个错误并得到集合视图应该用registerNib初始化的解决方案,但在我的编码中我已经这样做了。 我的viewDidLoad看起来像这样Ios7 ios 7中的-[UICollectionView\u dequeueReusableViewOfKind:with Identifier:forIndexPath:viewCategory:]中的断言失败,ios7,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios7,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我在使用两个UICollectionView时遇到此错误。 我搜索这个错误并得到集合视图应该用registerNib初始化的解决方案,但在我的编码中我已经这样做了。 我的viewDidLoad看起来像这样 UINib *cellNib =[UINib nibWithNibName:@"myCell" bundle:nil]; [self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myCell
UINib *cellNib =[UINib nibWithNibName:@"myCell" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myCell"];
UICollectionViewFlowLayout *horizontalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[horizontalFlowLayout setItemSize:CGSizeMake(300, 150)];
[horizontalFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.horizontalCollectionView setCollectionViewLayout:horizontalFlowLayout];
UINib *cellNib1 =[UINib nibWithNibName:@"myCell1" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myCell"];
UICollectionViewFlowLayout *verticalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[verticalFlowLayout setItemSize:CGSizeMake(150, 150)];
[verticalFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
NSArray *a=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
[self.verticalCollectionView setCollectionViewLayout:verticalFlowLayout];
self.dataArray=[[NSArray alloc]initWithObjects:a, nil];
cellForItemAtIndexPath方法如下所示
if (collectionView==self.horizontalCollectionView)
{
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"myCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
titleLabel.textColor=[UIColor whiteColor];
[titleLabel setText:cellData];
return cell;
}
else
{
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier1 = @"myCell";
UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier1 forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell1 viewWithTag:2];
titleLabel.textColor=[UIColor whiteColor];
[titleLabel setText:cellData];
return cell1;
}
初始化cell1时出错
完整的错误信息如下所示。
*在-[UICollectionView\u dequeueReusableViewOfKind:with Identifier:forIndexPath:viewCategory:,/SourceCache/UIKit\u Sim/UIKit-2935.137/UICollectionView.m:3241中断言失败,我刚刚收到了这个消息。对我来说,问题是:
nib中的集合可重用视图标识符设置为与我在代码中注册的标识符不同的标识符
[self.collectionView registernb:nib forCellWithReuseIdentifier:aNib]代码>所以我总共花了3个小时处理这个愚蠢的问题。对我来说,这是不好的,因为除了这个细胞,其他细胞的工作方式与我完全相同。这一错误具有误导性。对我来说,这是因为我在xib有一个手势识别器。。。是的,它甚至不需要链接。一旦我把它拿出来,它就起作用了。我最后只是通过代码添加了手势识别器。
奇怪,因为它适用于自定义UITableViewCell,但不适用于自定义UICollectionViewCell
希望这对任何人都有帮助 您应该在viewDidLoad方法中添加以下行:
UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CollCell"];
其中,CollectionViewCell是自定义CollectionViewCell nib文件的名称,CollCell是用于在(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath方法中将单元格出列的标识符
我希望这对您有所帮助-您是否对cell使用了Xib或故事板?
- 我发现如果我使用了Xib,并且在Xib中将其命名为标识符“cell”
- 但是我用了
[self.collectionView注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:@“reuseIdentifier”]在我的代码中
- 这架飞机坠毁了
我在代码中将xib的标识符更改为相同的标识符,它也会运行 错误消息是否说明哪种断言失败?(请将完整的错误信息粘贴到您的问题中。)那么我的前两行代码是什么??我使用该标识符重用cellForItemAtIndex方法中的单元格