Question

I am having trouble getting this code to run after having it crash after several iterations. The intended behaviour is to simply display images in a Collection View from an array. If I comment out all the objects in the array, it runs with an empty collection view. logo.png exists and can be properly loaded in other parts of the app through the dropdown. Delegate and DataSource is properly set to self. The Collection View has a single cell displayed with an ImageView in it (tagged as 100) and nothing else. Having a label instead of an image view causes a crash with any objects in the array as well.

The debugging output is

Could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must
register a nib or a class for the identifier or connect a prototype cell in a storyboard

The relevant code is the following:

ViewController.m

#import "demo_frameworkViewController.h"

@interface demo_frameworkViewController ()

@end

@implementation demo_frameworkViewController {
    NSMutableArray *imageArray;
}


@synthesize imageArray;


- (void)viewDidLoad
{
    imageArray = [[NSMutableArray alloc] init];

    [imageArray addObject:[UIImage imageNamed:@"logo.png"]];
    [imageArray addObject:[UIImage imageNamed:@"logo.png"]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Collection View Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return [self.imageArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UIImageView *imageDemo = (UIImageView *)[cell viewWithTag:100];

    imageDemo.image =[imageArray objectAtIndex:indexPath.row];
    [cell.layer setBorderWidth:2.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];
    return cell;


}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface demo_frameworkViewController : UIViewController <UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>


@property (nonatomic, retain) NSArray *imageArray;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@end
Was it helpful?

Solution

The debugging output means that it cannot find a cell called 'Cell'. In your storyboard you need to set the Collection Reusable View value of the UICollectionViewCell to 'Cell' so that the UICollectionView knows what cell to use

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top