7

我试图让一个UICollectionView在模态呈现的视图控制器内显示。该应用程序是iPad等iOS 7无法获取UICollectionView来显示单元格

我创建了子类的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; 
} 

我还设置了UICollection的子类ViewCell的标识符设置为MyCell,并添加到它的标签100的UIImageView。

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

+0

[你遵循Appcoda教程吗?](http://www.appcoda.com/ios-collection-view-tutorial/) –

回答

20

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

接下来,你需要注册UICollectionViewCell:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Register Nib 
    [self.collectionView registerNib:[UINib nibWithNibName:CollectionViewCell_XIB bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CollectionViewCell_ID]; 
} 

CollectionViewCell_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; 
} 
+1

完全真棒,先生。谢谢! – beev

+3

不客气;)毫不犹豫地加+1:p –

24

另一个值得关注的问题是,如果您设置了单元的标识符在笔尖或故事板中,不要在集合视图控制器中注册笔尖/类。做一个或另一个,但不是两个。

+4

令人惊讶的是,我遇到了同样的问题,这解决了它。 –

+3

这是我的问题。 Thx –

+0

这个!!!! omg我不敢相信 – SteBra

相关问题