问题描述
我在这里遗漏了一些明显的东西,因为这不是一项艰巨的任务.
我在故事板中有我的集合视图(以及其他视图),原型单元格的重用 ID 为单元格",该单元格中有一个 UILabel,标签为 100.
代码(样板):
I'm missing something obvious here as this isn't a difficult task.
I have my Collection view in storyboard (amongst other views), with the prototype cell's reuse id being "Cell", and a UILabel in that cell, with a tag of 100.
The code (boilerplate):
- (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
中.当我运行时,视图出现但没有单元格或文本可见.
The code is within a UICollectionViewController
, as it should.When i run, the view appears yet no cells or text are visible.
A NSLog
确认它被调用了 4 次.我什至更改了原型单元格的背景颜色,这表明甚至没有出现单元格.
A NSLog
in the cellForItemAtIndexPath
confirms that it is called, 4 times.I've even changed the prototype cell's background color which shows that not even the cell's are appearing.
在viewDidLoad
中调用:[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
我错过了什么?
推荐答案
像这样使用:代替 registerClass:使用 registerNib:
Use like this:Instead of 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];
}
这篇关于iOS UICollectionView:单元格未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!