Home Ask Login Register

Developers Planet

Your answer is one click away!

Houman February 2016

How to use custom UICollectionViewCell within Storyboard?

I don't understand why the custom cell is not recognised in the Storyboard.


static NSString * const reuseIdentifier = @"TNTopNewsCellItem";

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    TNTopStoriesCollectionViewCell *cell = (TNTopStoriesCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // Configure the cell
    [self preLoadImagesForCategory:_dataStore.fiveTopStories[indexPath.item] andImageView:cell.itemImage];
    return cell;

I'm using a storyboard for the collectionView: enter image description here

And the cell is also correctly pointing to the custom class: enter image description here

#import <UIKit/UIKit.h>

@interface TNTopStoriesCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *itemImage;
@property (weak, nonatomic) IBOutlet UILabel *title;


And the correct cell identifier is also used: enter image description here

And yet I get this exception within cellForItemAtIndexPath method:

[UICollectionViewCell itemImage]: unrecognized selector sent to instance

But why please?


I did everything as suggested and am now getting this: enter image description here



matt February 2016

The fact that this cell is called "Collection View Cell" on the left, in the hierarchical name listing of objects in the scene, and has not changed to "Top Stories Collection View Cell" suggests that you have not actually changed its class in the storyboard to TNTopStoriesCollectionViewCell successfully.

(Notice how the collection view controller is listed on the left as Top Stories Collection View Controller. The same kind of thing should have happened to the cell.)

What you see on the left should look more like this:

enter image description here

(Also, the fact that your label outlet is called News Label on the left is suggestive of a problem, since in code this label is called "title", and if you've hooked that up correctly I'd expect Xcode to have changed its name to "title" in the listing on the left as well.)

[I have a vague feeling that you may actually have two classes, TNTopStoriesCollectionViewCell and TNTopNewsCellItem, and you've confused yourself about them...]

user3182143 February 2016

In storyboard, there is another way to create Custom CollectionViewCell.That is Separately create the new file(CustomCellCollectionViewCell.h,CustomCollectionViewCell.m and CustomCollectionViewCell.xib) through the IOS->Source->Cocoa Touch Class->SubClass of UICollectionViewCell and give name in Class. After that you can import the the .h of CustomCollectionViewCell and also you need to register that in viewDidLoad.

- (void)viewDidLoad 
   UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
   flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
   UINib *cellNib = [UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil];
   [collectionViewSelection registerNib:cellNib forCellWithReuseIdentifier:@"customCollectionCell"];


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
   static NSString *cellIdentifier = @"customCollectionCell";
   CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

   return cell;

Post Status

Asked in February 2016
Viewed 3,772 times
Voted 14
Answered 2 times


Leave an answer

Quote of the day: live life