我有一個自定義UICollectionViewCell
具有自定義背景視圖,使用幾種配色方案之一繪製。背景視圖的配色方案在我的自定義初始化程序-(id)initWithFrame:andColourPalette:
中設置。初始化自定義UICollectionViewCell
我有一個類似的自定義初始化器在我UICustomViewCell
子類,但我無法弄清楚如何調用這個初始化器,當我建立細胞在cellForItemAtIndexPath:
誰能幫助我做到這一點?或者提供另一種解決方案,將這種顏色字典傳遞給Cell以傳遞給子視圖?
編輯以顯示更多細節:
這是我在我的UICollectionView VC:
在viewWillAppear中:
[self.collectionView registerClass:[OPOLawCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
self.colourPalette = [OPOColourPalette greenyColourPalette];
在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;
在我的Custom UICol中lectionViewCell
- (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];
但這不起作用 - 我猜是因爲propertys沒有設置。
如果我最後一行改變這一點,那麼它工作正常:
OPOLawBook *lawBookView = [[OPOLawBook alloc]initWithFrame:CGRectMake(0, 0, 200, 265) andColourPalette:[OPOColorPalette greenyColorPalette]];
所以我想我需要在這裏使用自定義intialiser,但我不知道如何稱呼它,或從那裏。 ..
感謝
我已經在做這一點,但我要告訴小區什麼顏色繪製背景圖片。如果我在colourPalette的Cell上放置一個屬性,然後在cellForItemAtIndexPath中設置它:它不起作用,因爲該屬性在單元格繪製背景之前未設置。所以我想我需要重寫initWithFrame:所以它使用我的initWithFrame:andColourPalette:方法。 – Bertie
我已經提出了一些代碼在這個問題上給予更多的細節 – Bertie
你不應該做任何綁定到單元格索引的初始化。您需要能夠在將該單元出隊後進行這些自定義。 – Tala