hvgotcodes
hvgotcodes

Reputation: 120198

iOS -- missing something adding a custom UICollectionViewCell to a UICollectionView

Im trying to define my own custom UICollectionViewCell implementation and use it in a UICollectionView. Here is what I have done.

1- I am using storyboards.

2- I added a UICollectionView to another view, that was a scene on the storyboard.

3- I pointed datasource and delegate of the collection to the controller for the scene.

4- I provided implementations of the required methods in the controller.

5- I created a cell view .h and .m file. In the .h file I do

@interface HscCellView : UICollectionViewCell

6- I created a new view, so I got a new canvas, and in the appropriate section on the right I specified my class name (same as in step 5), and provided a unique identifier.
7- In the controller for the scene, I defined an outlet in the .h file to the collection view I had dragged in.

@property (nonatomic, strong) IBOutlet UICollectionView *holesCollectionView;

and in viewDidLoad I do

[self.holesCollectionView registerClass:[HscCellView class] forCellWithReuseIdentifier:@"cellView"];

note that cellView is the unique identifier I specified for my custom cell xib.

8- Finally, I implemented

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HscCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellView" forIndexPath:indexPath];

    return cell;

}

When I fire up the app, I get a bunch of cell views (9, since I specify 9 on the numberOfItemsInSection message) but they are not my custom cell view. They don't have the size, color, or label. I'm not seeing any errors. I'm stuck.

A few ancillary questions.

Upvotes: 4

Views: 3026

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120198

Got it, I think. I'll try to explain it and hopefully will be corrected if I'm wrong. I think this will be helpful for others. Based on the comments from my question, I determined that instances of my class were in fact being loaded. However, the xib was being ignored. This implied that while my class was loaded, the xib was not being used. So, in step 7 in the question, I do

UINib *nib = [UINib nibWithNibName:@"HscCellView" bundle:nil];
    [self.holesCollectionView registerNib:nib forCellWithReuseIdentifier:@"cellView"];

Instead of the registering the class, and now things appear to work.

I am kind of curious as to why, since I linked the xib with my custom class, this is necessary, but I am no longer stuck.

Upvotes: 11

Related Questions