iOS子类UICollectionViewCell自定义init方法

Roh*_*wal 11 iphone ios uicollectionview uicollectionviewcell

我正在实现一个自定义的UICollectionViewCell,我想知道如何向它发送模型数据,以便数据可用于初始化单元的子视图.

我通过做注册我的MyCollectionViewCell

[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)

在以下方法中,我做...

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

MyCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

我面临的问题是,当一个单元不在重用队列中时,它通过调用initWithFrame方法(文档确认应该发生)来初始化一个新单元.但是,我的MyCollectionViewCell类中有一个自定义的init方法,它是......

-(id) initWithFrame:(CGRect)frame withData: (NSDictionary*) data;
Run Code Online (Sandbox Code Playgroud)

我希望调用自定义初始化方法.我该怎么办?我没有使用任何笔尖,我正在以编程方式执行所有操作.

如果没有办法,我必须使用initWithFrame方法,我还能如何将模型数据传递给MyCollectionViewCell,以便我可以用该数据初始化子视图?

谢谢!

Phi*_*ell 6

你出列后,[cell initData:data]怎么样?无论如何,您可能需要重新启动出列单元格的数据.