根据数据更改UICollectionViewCell内容和笔尖布局

我有一个UICollectionView显示许多UICollectionViewCells,我已经被分类为CardCell。 我将variables“types”传递给- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath我希望CardCell类能够加载不同的Nib文件,具体取决于传递的types英寸不同的types需要有不同的布局。

问题是我无法弄清楚在我的CardCell.m中改变这个地方。 我尝试使用- (void)prepareForReuse但除非用户正在滚动,否则不会调用。

你应该在viewDidLoad中注册你需要的每个nib文件,像这样(用nib文件和标识符replace正确的名字):

 [self.collectionView registerNib:[UINib nibWithNibName:@"RDCell" bundle:nil] forCellWithReuseIdentifier:@"FirstType"]; 

然后,在itemForRowAtIndexPath中,testingtypes并返回正确types的单元格:

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (type = @"firstType") { FirstCell *cell = (FirstCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstType" forIndexPath:indexPath]; return cell; }else{ SecondCell *cell = (SecondCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"SecondType" forIndexPath:indexPath]; cell.whatever ..... return cell; } }