简体   繁体   English

UICollectionView中未声明的标识符错误

[英]Undeclared identifier error in UICollectionView

This is my ViewController.m file's code for Collection view: 这是我的ViewController.m文件的Collection视图代码:

- (void)viewDidLoad
{
     img = [[NSArray alloc] initWithObjects:@"1.png",@"2.png",@"3.png", nil];
     name = [[NSArray alloc] initWithObjects:@"Flag",@"Blue",@"Fish", nil];
     [self.collectionview registerNib:[UINib nibWithNibName:@"cell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [img count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell1 = [collectionview dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    UIImageView *reciveimageview = (UIImageView *)[cell1 viewWithTag:100];
    reciveimageview.image = [UIImage imageNamed:[img objectAtIndex:indexPath.row]];
    cell1.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[img objectAtIndex:indexPath.row]]];
    cell1.cellLabel.text = [name objectAtIndex:indexPath.row];
    return cell1;
}

@end

Now it is giving me the error of Undeclared identifier of Cell and Cell1 Don't know why. 现在它给我一个Undeclared identifier of Cell and Cell1的错误不知道为什么。

Add like this static NSString *identifier = @"Cell"; 像这样添加静态NSString * identifier = @“Cell”;

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView *recipeImageView=(UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[jsonImagesArr objectAtIndex:indexPath.row]];
[cell addSubview:recipeImageView];

Make sure the cell on the .xib file know what's the type of the cell. 确保.xib文件上的单元格知道单元格的类型。

Select the cell on your interface builder 在界面构建器上选择单元格

在此输入图像描述

and then on the identity inspector. 然后在身份检查员身上。 In your case it should be cell 在你的情况下,它应该是cell

在此输入图像描述

Have you set the identifier in xib as "CELL" 您是否已将xib中的标识符设置为“CELL”

在此输入图像描述

You need to register your nib/class if you are using a custom cell. 如果使用自定义单元格,则需要注册nib / class。 You can do that by 你可以做到这一点

[self.myCollectionView registerNib:[UINib nibWithNibName:@"ShubhCalendarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ShubhCalendarCollectionViewCell"]; 

Make sure your Nib name and identifier are not mis- spelled. 确保您的笔尖名称和标识符没有错误拼写。 Hope it helps. 希望能帮助到你。

Happyy coding.. !! 快乐编码.. !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM