Reputation: 8579
I'm new to UICollectionViews. I have setup a basic UICollectionViewController with rows of 2 cells. In the controller I have one cell and I dragged a UIImageView into it. I connected it to the custom cell class. I made sure so change the class of the cell to this one in the interface builder also. In my view controller I have this code:
@implementation StickersViewController
static NSString * const reuseIdentifier = @"stickerCell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
//Add the images
self.images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"one.png"], [UIImage imageNamed:@"two.png"], [UIImage imageNamed:@"three.png"], nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.images count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
StickerCell *cell = (StickerCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.imageView.image=self.images[indexPath.row];
return cell;
}
I am getting this error when running the code: UICollectionViewCell imageView]: unrecognized selector sent to instance 0x7fe8e0e7828. Not sure why. Seems I have connected things up correctly. Could someone give me some pointers to where I might be going wrong please?
Upvotes: 0
Views: 520
Reputation: 2904
refer this tutorial http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
you will get the right answer. probably i think you don't set the collection reusable view identifier
select collection view cell goto attribute inspector and set the collection reusable view identifier = cell
Upvotes: 1
Reputation: 35616
The problem is here:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
where you're registering a UICollectionViewCell
instead of your custom subclass. So that should be:
[self.collectionView registerClass:[StickerCell class] forCellWithReuseIdentifier:reuseIdentifier];
Also, keep in mind that in case that the setup is done in a Storyboard you do not need to register the class yourself since it is automatically handled for you (provided that you have set a custom class & a reuse identifier)
I hope that this makes sense
Upvotes: 4