iOS:集合视图不显示
我在我的应用程序中有一个集合视图,我希望它包含一个自定义单元格。我已经创建了一个自定义单元格视图xib文件。然后,我用它在我的数据源方法:iOS:集合视图不显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath];
if(cell == nil){
NSArray *nsObjects = [[NSBundle mainBundle] loadNibNamed:@"OtherCustomersCell" owner:nil options:nil];
for(id obj in nsObjects)
if([obj isKindOfClass:[OtherCustomersCell class]])
cell = (OtherCustomersCell*) obj;
}
[cell.name setText:@"AAAA BBBBB"];
return cell;
}
但是当我运行的应用程序中,只有一个黑色的长方形,其中集合视图应该是(在表视图下的底部):
我在做什么错? 预先感谢您。
集合视图的工作方式与表视图的不同之处在于,如果无法取出队列,则不必创建单元格。
相反,你必须先注册该小区的笔尖:
- (void)viewDidLoad
{
...
UINib *cellNib = [UINib nibWithNibName:@"OtherCustomersCell" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER];
}
然后就可以出列的单元格,它会自动为你,如果有必要创建:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath]; // cell won't be nil, it's created for you if necessary!
[cell.name setText:@"AAAA BBBBB"];
return cell;
}
谢谢你的详细答案! – RCB 2013-02-28 10:00:38
沿着这些线,它应该是: UINib * cellNib = [UINib nibWithNibName:@“OtherCustomersCell”bundle:nil]; [self.photoGridView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER]; – kevinl 2013-08-01 19:46:54
谢谢,这个解决方案真的适合我! – acoustic 2014-01-22 09:28:15
你有以下面的方式在viewdidload中注册UICollectionView
实例,那么你可以使用它。
[self.photoListView registerNib:[UINib nibWithNibName:@"UIcollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Identifier"];
你注册了吗? – Exploring 2013-02-28 09:47:02
@sanjitshaw你在问收集视图吗?我通过故事板文件添加了它。或者自定义单元格?比没有,但我正在做我的表视图相同的过程,并且它工作正常(正如你所看到的) – RCB 2013-02-28 09:50:27
我已经发布了一个答案检查 – Exploring 2013-02-28 09:55:14