UICollectionView显示为空白 - 单元格未显示

时间:2015-11-10 23:30:15

标签: ios objective-c xcode uicollectionview xib

我已经使用自定义单元格XIB设置了UICollectionView。我已经适当地设置了委托,数据源方法,但在加载时,集合视图显示为空白,黑屏:

blank

以下是我注册自定义xib单元格的代码:

    [self.collectionView registerClass:[CustomViewCell class] forCellWithReuseIdentifier:@"cvCell"];

委托/数据源方法:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView    {
return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView  numberOfItemsInSection:(NSInteger)section {
return 4; }

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

static NSString *cellIdentifier = @"cvCell";
CustomViewCell *cell = (CustomViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

return cell;

}

我还为自定义xib单元格设置了类:

class_xib_cell

编辑:

集合视图布局设置为:

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(50, 29)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

[self.collectionView setCollectionViewLayout:flowLayout];

50,29指的是实际自定义视图xib单元格的大小

1 个答案:

答案 0 :(得分:0)

您应该注册自定义单元格的笔尖而不是类。

UINib *cellNib = [UINib nibWithNibName:@"cvCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];