无法获取UICollectionView以显示单元格

我试图让一个UICollectionView显示在一个模态的视图控制器。 该应用程序是为iPad的iOS 7。

我已经创build了UIViewController的子类(用一个笔尖),并像这样添加它:

MyViewController *controller = [[MyViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; navController.modalPresentationStyle = UIModalPresentationFullScreen; navController.navigationBar.barStyle = UIBarStyleBlackTranslucent; [self presentViewController:navController animated:YES completion:nil]; 

这个视图控制器是我的UICollectionView的委托和数据源,所以我已经添加了UICollectionViewDataSource和UICollectionViewDelegate头。

我已经把一个UICollectionView放入笔尖,并添加了一个出口MyViewController:

 @property (strong, nonatomic) IBOutlet MyCollectionView *collectionViewController; 

我已经在MyViewController的viewDidLoad中添加了这个:

 self.collectionViewController.dataSource = self; self.collectionViewController.delegate = self; 

我还将以下内容添加到MyViewController中:

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSLog(@"Items in section: %d", itemsArray.count); // returns correct amount return itemsArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForItemAtIndexPath %@", indexPath); // returns as expected static NSString *identifier = @"MyCell"; [self.collectionViewController registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:identifier]; MyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; UIImageView *myImageView = (UIImageView *)[cell viewWithTag:100]; myImageView.image = [UIImage imageNamed:[itemsArray objectAtIndex:indexPath.row]]; return cell; } 

我还设置了一个UICollectionViewCell的子类,标识符设置为MyCell,并添加了一个标签为100的UIImageView。

每当我调出这个视图控制器的时候,我都会像预期的那样得到导航条,但是我添加到我的笔尖的UICollection视图是无处可见的。 所有我看到的是收集视图应该是黑色的。 如果我将MyCollectionView的背景颜色从默认值更改为白色,我会在集合视图应该显示的地方看到白色。 这似乎是提出了MyCollectionView,但没有显示任何单元格。

如果你在你的xib文件中链接你的collectionView和它自己的dataSource和delegate,你不需要在你的代码中设置它。

接下来,你需要注册你的UICollectionViewCell:

 - (void)viewDidLoad { [super viewDidLoad]; // Register Nib [self.collectionView registerNib:[UINib nibWithNibName:CollectionViewCell_XIB bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CollectionViewCell_ID]; } 

CollectionViewCell_XIB是您的单元格的名称xib CollectionViewCell_ID是您的单元格的ID

你需要像这样实现cellForItemAtIndexPath

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCell_ID forIndexPath:indexPath]; // Configure cell with data UIImageView *myImageView = (UIImageView *)[cell viewWithTag:100]; myImageView.image = [UIImage imageNamed:[itemsArray objectAtIndex:indexPath.row]]; // Return the cell return cell; } 

还有一点需要注意的是,如果您在单元格或故事板中设置单元格的标识符,请不要在集合视图控制器中注册nib / class。 做一个或另一个,但不是两个。