初始化自定义UICollectionViewCell [英] Initializing a custom UICollectionViewCell

查看:352
本文介绍了初始化自定义UICollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 UICollectionViewCell ,它有一个自定义背景视图,使用多种配色方案之一绘制。背景视图的颜色方案在我的自定义初始值设定项 - (id)initWithFrame:andColourPalette:中为视图设置。

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.

我的 UICustomViewCell 子类中有一个类似的自定义初始化程序,但是当我在<$ c $中设置单元格时,我无法弄清楚如何调用此初始化程序c> cellForItemAtIndexPath:

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?

编辑以显示更多详细信息:

这是我在UICollectionView VC中的内容:

This is what I have in my UICollectionView VC:

在ViewWillAppear中:

In ViewWillAppear:

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

在cellForItemAtIndexPath中:

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;

在我的自定义UICollectionViewCell

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]];

所以我想我需要在这里使用自定义的intialiser,但我无法弄清楚如何调用它,或者从哪里......

So i guess I need to use a custom intialiser here but I cant figure out how to call it , or from where...

谢谢

推荐答案

Yuo必须在collectionView中注册customCells:

Yuo have to register your customCells in collectionView:

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

然后在你的方法中 cellForItemAtIndexPath

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

这样做是因为collectionView可能有1000个单元格,10个可见。如果可能,您不会将所有这些内容初始化并重复使用。

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

编辑

在解除可重用单元格后,应该设置 colorPaletter 。可以把它想象成一个可以容纳任何颜色的容器。您需要确定(通过索引路径)要绘制的颜色。

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.

这篇关于初始化自定义UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆