Initializing a custom UICollectionViewCell

2019-05-06 04:14发布

问题:

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

I have a similar custom initialiser in my UICustomViewCell subclass but I can't figure out how to call this initialiser when I am setting up the cell in cellForItemAtIndexPath:

Can anyone help me do this? Or offer alternative solution for passing this Dictionary of colours into the Cell to pass on to the subView?

EDIT to show more detail:

This is what I have in my 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 that doesn't work - I guess because the propertys are not set up.

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 guess I need to use a custom intialiser here but I cant figure out how to call it , or from where...

Thanks

回答1:

Yuo have to register your customCells in collectionView:

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

And then in your method cellForItemAtIndexPath:

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

It is done because collectionView might have 1000 cells and 10 visible. You don't keep all of them initialized and reuse when possible.

EDIT

You should set colorPaletter after you deque the reusable cell. Think of it as a container which can hold any color. You need to determine (by indexpath) what color to paint.



回答2:

You shouldn't do below if your custom cell is in the Storyboard,

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

Because Storyboard take responsibility to register Cell_ID own. Now, It will conflict to be generated invalid Cell if you use both.



回答3:

Way off, every answer. The questioner is looking for a way to uniquely identify each cell upon initialization, which happens prior to dequeuing a cell, and prior to a cell's access to its index path property.

The only way to do this is to assign a unique reuse identifier to every cell based on what the index path value will be (assuming you will know what that will be—and, in your case, you will); then, when dequeuing the cell, use the index path to find the cell with the corresponding reuse identifier.

Does this negates the purpose of reuse identifiers? Absolutely not. You'll be reusing that cell every time you need to use it again. Reuse identifiers were not meant to limit you to a cookie-cutter cell for every cell in your collection view; they are also intended to be "unique use" identifiers.