未调用cellForItemAtIndexPath

时间:2012-11-21 09:31:49

标签: objective-c xcode cocoa collectionview

我正在尝试从视图控制器推送到集合视图控制器。

UICollectionViewLayout *collectionViewLayout = [[UICollectionViewLayout alloc] init];
xraylab *xraylabvc = [[xraylab alloc]initWithCollectionViewLayout:collectionViewLayout];
[self.navigationController pushViewController:xraylabvc animated:YES];

集合视图控制器将显示一个黑色的空白屏幕。

numberOfSectionsInCollectionView返回1

numberOfItemsInSection返回非零整数

我的问题是cellForItemAtIndexPath未被调用。

设置集合视图的委托和数据源。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

numberOfItemsInSectionreturn rowcount > 0执行cellForItemAtIndexPath

检查Delegate的{​​{1}}和datasourcecollection view

答案 1 :(得分:1)

我遇到了同样的问题,这让我感到很疯狂,这是有效的代码。

UICollectionViewFlowLayout *CollLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *AttachmentsView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, CurrentY, 640, 300) collectionViewLayout:CollLayout];
//AttachmentsView.backgroundColor = [UIColor whiteColor];
[AttachmentsView registerClass:[Cell class] forCellWithReuseIdentifier:@"ReuseCell"];
[VaribleStore SharedInstance].Attachments = [[NSMutableArray alloc] initWithObjects:[UIColor yellowColor],[UIColor redColor],[UIColor blueColor], nil];
AttachmentsView.delegate = self;
AttachmentsView.dataSource = self;
[self.ScrollView addSubview:AttachmentsView];
[AttachmentsView reloadData];

关键是要使用UICollectionViewFlowLayout,原因是UICollectionViewLayout是一个抽象类,不能用于此目的。我做了那个切换ios后正常称为cellForItemAtIndexPath。

答案 2 :(得分:0)

您不能按原样使用UICollectionViewLayout。它是一个抽象的基类,必须是子类。如果您不想编写自己的视图,可以将UICollectionViewFlowLayout用于类似网格的视图。

相关问题