Initializing a Custom UICollectionViewCell

I have a custom UICollectionViewCell that has a custom background view that is drawn using one of several color schemes. The color scheme for the background view is set in my custom initializer -(id)initWithFrame:andColourPalette: for the view.

I have a similar custom initializer in my subclass of UICustomViewCell , but I cannot figure out how to call this initializer when I set up the cell in cellForItemAtIndexPath:

Can someone help me do this? Or suggest an alternative solution for passing this Color Dictionary to a cell to go to subView?

EDIT to show more details:

This is what I have in the UICollectionView VC:

In ViewWillAppear:

 [self.collectionView registerClass:[OPOLawCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID]; self.colourPalette = [OPOColourPalette greenyColourPalette]; 

In cellForItemAtIndexPath:

 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath]; OPOLawCollectionViewCell *lawCell = (OPOLawCollectionViewCell *)cell; MainLevel *level = self.collectionData[indexPath.row]; lawCell.delegate = self; lawCell.colourPalette = self.colourPalette; 

In my custom UICollectionViewCell

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // get background view OPOLawBook *lawBookView = [[OPOLawBook alloc]initWithFrame:CGRectMake(0, 0, 200, 265) andColourPalette:self.colourPalette]; 

But this does not work - I think, because the properties are not configured.

If I change the last line to this, then it works fine:

  OPOLawBook *lawBookView = [[OPOLawBook alloc]initWithFrame:CGRectMake(0, 0, 200, 265) andColourPalette:[OPOColorPalette greenyColorPalette]]; 

So, I think I need to use a custom intialiser here, but I can't figure out what to call it, or where ...

thanks

+4
source share
3 answers

Yuo needs to register your customCells in the View collection:

 [self.collectionView_ registerClass:[YourCustomClass class] forCellWithReuseIdentifier:@"CustomCell"]; 

And then in your cellForItemAtIndexPath method:

  YourCustomClass *cell = (YourCustomClass *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath]; 

This is because collectionView can have 1000 cells and 10 visible. You do not keep all of them initialized and reusable whenever possible.

EDIT

You must set colorPaletter after removing the reuse cell. Think of it as a container that can contain any color. You need to determine (by pointer) which color to draw.

+12
source

You should not do below if your custom cell is in the Storyboard ,

 [self.collectionView registerClass:[OPOLawCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID]; 

Because Storyboard is responsible for registering Cell_ID own. Now it will conflict with an invalid cell if you use both.

+2
source

Refuse, every answer. The questionnaire is looking for a way to uniquely identify each cell during initialization, which occurs before the cell is deleted, and before the cell gains access to the index path property.

The only way to do this is to assign a unique reuse identifier for each cell based on what the index path value will indicate (assuming you will know what it will be - and, in your case, you will) then when deleting a cell, use the pointer path to search for the cell with the corresponding reuse identifier.

Is this marked for reuse purposes? Absolutely not. You will reuse this cell every time you need to use it again. Reuse identifiers should not limit you to the cookie-cookie cell for each cell in your collection view; they must also be "unique use" identifiers.

+1
source

Source: https://habr.com/ru/post/1489111/


All Articles