iOS UICollectionView:单元格未显示

Sim*_*on. 2 objective-c ios 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;
}
Run Code Online (Sandbox Code Playgroud)


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

一个NSLogcellForItemAtIndexPath确认它被称为4倍.我甚至改变了原型细胞的背景颜色,这表明甚至细胞都没有出现.

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

我错过了什么?

Pra*_*tel 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];
}
Run Code Online (Sandbox Code Playgroud)