繁体   English   中英

iOS UICollectionView:单元格未显示

[英]iOS UICollectionView: Cells not showing

我在这里缺少明显的东西,因为这不是一项艰巨的任务。

我在情节提要中有“收藏夹”视图(以及其他视图),原型单元的重用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 当我运行时,该视图出现,但没有任何单元格或文本可见。

cellForItemAtIndexPathNSLog确认调用了4次。 我什至更改了原型单元的背景颜色,该颜色表明甚至没有该单元的出现。

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

我想念什么?

像这样使用:代替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];
}

如果您使用任何其他配置,并且尝试使用模拟可能无法显示的情况,请确保您在情节提要中处于任意比率

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM