iOS UICollectionView:单元格未显示

时间:2015-01-08 09:53:34

标签: ios objective-c uicollectionview uicollectionviewcell

我在这里遗漏了一些明显的东西,因为这不是一项艰巨的任务。

我在故事板(在其他视图中)中有我的Collection视图,原型单元的重用ID为“Cell”,并且该单元格中的UILabel,标记为100.

代码(样板) :

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


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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    UILabel *label = (UILabel *)[cell viewWithTag:100];
    label.text = @"Hello";

    return cell;
}


代码在UICollectionViewController范围内,应该如此。 当我运行时,视图显示没有单元格或文本可见。

NSLog中的cellForItemAtIndexPath确认已被呼叫4次。 我甚至改变了原型细胞的背景颜色,这表明甚至细胞都没有出现。

viewDidLoad中,它会调用:[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

我缺少什么?

2 个答案:

答案 0 :(得分:6)

像这样使用: 而不是registerClass:使用registerNib:

static NSString *identifier = @"Cell";
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
    [self.collection registerNib:[UINib nibWithNibName:@"CollectionPhotoItem" bundle:nil] forCellWithReuseIdentifier:identifier];
}
else{
    [self.collection registerNib:[UINib nibWithNibName:@"CollectionPhotoItem_ipad" bundle:nil] forCellWithReuseIdentifier:identifier];
}

答案 1 :(得分:0)

确保您在故事板中处于任何比例之下 如果您正在使用任何其他配置,并且您尝试使用模拟它可能不会显示

例如,如果您使用常规宽度和紧凑高度然后尝试在Xcode中使用模拟器,则所有工作都不会显示出来!

相关问题