我在同一个 Controller 中使用了两个 UICollectionView
但是当我初始化第二个 UICollectionView
时它给出了类似
由于未捕获的异常 NSInternalInconsistencyException
而终止应用程序,原因:无法使类型的 View 出队:具有标识符单元格的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接一个原型(prototype) Storyboard中的单元格
这是我的代码:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView==collView) {
return arrDatabase.count;
}
else
return arrSearchResult.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier=@"cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (collectionView==collView) {
objprop=[arrDatabase objectAtIndex:indexPath.row];
UIImageView *imgView=(UIImageView*)[cell viewWithTag:100];
//imgRecipe.image=[UIImage imageNamed:[ objectAtIndex:indexPath.row]];
imgView.image=[UIImage imageNamed:@"Hamburger.jpg"];
UILabel *lblName=(UILabel*)[cell viewWithTag:101];
lblName.text=[NSString stringWithFormat:@"%@",objprop.categoryName];
//cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
}
if (collectionView==collView1) {
NSString *str=@"setIdentifier";
UIImageView *imgView2;
UILabel *lblCat;
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
if (cell==nil) {
cell
=[[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 97, 101)];
imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(2, 13, 92, 76)];
lblCat=[[UILabel alloc]initWithFrame:CGRectMake(3, 94, 92, 13)];
}
imgView2.image=[UIImage imageNamed:@"hamburger.jpg"];
[cell.contentView addSubview:imgView2];
lblCat.backgroundColor=[UIColor clearColor];
lblCat.text=[NSString stringWithFormat:@"%@",[arrSearchResult[indexPath.row] objectAtIndex:0]];
[cell.contentView addSubview:lblCat];
}
return cell;
}
最佳答案
请在viewDidLoad
中注册UICollectionViewCell
类
喜欢:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
关于ios - UICollectionView 未注册单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794997/