Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Subclass UICollectionViewCell custom init method

I'm implementing a custom UICollectionViewCell, and I want to know how to send model data to it so the data can be used to initialize the subviews of the cell.

I register my MyCollectionViewCell by doing..

[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];

and in the following method, I do...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

MyCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

The problem I'm facing is, when a cell isn't in the reuse queue, it initializes a new cell by calling the initWithFrame method (which the documentation confirms should happen). However, I have a custom init method in my MyCollectionViewCell class, which is...

-(id) initWithFrame:(CGRect)frame withData: (NSDictionary*) data;

I want my custom initialize method to be called instead. How would I do that? I'm not using any nibs and I'm doing everything programmatically.

If there's no way, and I must use the initWithFrame method instead, how else can I pass model data to my MyCollectionViewCell so that I can initialize the subviews with that data?

Thanks!

like image 776
Rohan Agarwal Avatar asked Dec 22 '12 01:12

Rohan Agarwal


1 Answers

How about [cell initData:data] right after you dequeue it? You probably need to re-init the data on dequeued cells, anyway.

like image 125
Phil Mitchell Avatar answered Oct 22 '22 16:10

Phil Mitchell