Harshil
Harshil

Reputation: 29

Undeclared identifier error in UICollectionView

This is my ViewController.m file's code for Collection view:

- (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.

Upvotes: 1

Views: 506

Answers (3)

luckyShubhra
luckyShubhra

Reputation: 2751

Have you set the identifier in xib as "CELL"

enter image description here

You need to register your nib/class if you are using a custom cell. 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.. !!

Upvotes: 0

DilumN
DilumN

Reputation: 2897

Make sure the cell on the .xib file know what's the type of the cell.

Select the cell on your interface builder

enter image description here

and then on the identity inspector. In your case it should be cell

enter image description here

Upvotes: 1

Uma Madhavi
Uma Madhavi

Reputation: 4917

Add like this static 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];

Upvotes: 2

Related Questions