iOS UICollectionView:单元格不显示

我错过了一些明显的东西,因为这不是一件困难的事情。

我有故事板(其他视图)中的集合视图,原型单元格的重用标识为“单元格”,并在该单元格中具有100标签的UILabel。

代码(样板):

- (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 ,因为它应该。 当我运行,视图出现,但没有单元格或文本可见。

cellForItemAtIndexPath中的一个NSLog确认它被调用了4次。 我甚至改变了原型单元格的背景颜色,显示甚至没有出现单元格。

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

我错过了什么?

像这样使用:而不是registerClass:use 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]; } 

如果你正在使用任何其他configuration,并且你尝试使用它可能不会显示的模拟,那么确保你在storyboard中的任何比例

例如,如果你使用普通的宽度和紧凑的高度,然后尝试在Xcode中使用模拟器,所有的工作将不会显示出来!