IOS - missing something adding a custom UICollectionViewCell to a UICollectionView

I am trying to define my own implementation of UICollectionViewCell and use it in a UICollectionView . Here is what I did.

1- I use storyboards.

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

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

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

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

@interface HscCellView : UICollectionViewCell

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

@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 specified for my custom xib cell.

8- Finally, I implemented

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

When I run the application, I get a bunch of cellular views (9, since I specify 9 in the message numberOfItemsInSection ), but they are not my custom cell view. They have no size, color or label. I do not see any errors. I am stuck.

A few supporting questions.

  • When do I use the nib register and when do I register the class name? I saw how this was done in both directions, but it seems that you almost always finish both the code and the class definition code, is that true ?.

  • In step 6, when I specify a unique identifier in the canvas for the user view of the cells, does this mean that I do not need to register the class in viewDidLoad ?

+4
source share
1 answer
I guess I think. I will try to explain this and hopefully will be fixed if I am wrong. I think it will be useful to others. Based on the comments on my question, I decided that instances of my class are actually loading. However, xib was ignored. This implied that although my class was loaded, xib was not 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 registering a class, and now everything works.

I am very curious why, since I linked xib to my regular class, this is necessary, but I was no longer stuck.

+11
source

All Articles