将UICollectionViewCell加载到UIView

时间:2015-10-05 10:45:27

标签: ios uiview uiviewcontroller uicollectionview uicollectionviewcell

我试图从.xib加载UICollectionViewCell到UICollectionView,它也是在.xib中可重用的UIView。这个UIView将被加载到更多的UIViewControllers。我发现,我需要像这样的称重传感器:

[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];

但它不会加载它。 我在网上四处寻找答案,但我无法找到这个具体案例。

1 个答案:

答案 0 :(得分:3)

有两种不同的情况,

1)如果您正在使用故事板和自定义单元格。

2)Nib和简单的UIView(UIViews的层次结构)。

你好像是第二种情况, 首先,您需要创建一个UICollectionView实例(通过alloc init方法)。 然后使用以下方法注册持有单元格/视图的笔尖。

[self.collectionView registerNib:[UINib nibWithNibName:CELL_IDENTIFIER bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER];

然后您可以使用以下方法使用该Nib

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell;
cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
                                                                        forIndexPath:indexPath];
//Populate your data here.
return cell;

}

希望这会有所帮助.. :)