无法让 UICollectionView 显示单元格 [英] Can't get UICollectionView to display cells

查看:44
本文介绍了无法让 UICollectionView 显示单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 UICollectionView 显示在模态呈现的视图控制器中.该应用适用于 iPad iOS 7.

I'm trying to get a UICollectionView to display inside a modally presented view controller. The app is for iPad iOS 7.

我创建了 UIViewController 的子类(带有笔尖)并添加如下:

I've created subclass of UIViewController (with a nib) and added it like this:

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.

This view controller is to be the delegate and dataSource for my UICollectionView so I've added UICollectionViewDataSource and UICollectionViewDelegate to the header.

我已将 UICollectionView 放入笔尖并为 MyViewController 添加了一个插座:

I've put a UICollectionView into the nib and added an outlet to MyViewController:

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

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

I've added this in viewDidLoad in MyViewController:

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

我还在 MyViewController 中添加了以下内容:

I've also added the following to 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.

I've also set up a subclass of UICollectionViewCell with the identifier set to MyCell and added to it a UIImageView with the tag 100.

每当我打开这个视图控制器时,我都会按预期获得导航栏,但是我添加到我的笔尖的 UICollection 视图无处可见.我所看到的只是集合视图应该是黑色的.如果我将 MyCollectionView 的背景颜色从默认更改为白色,我会在集合视图应该出现的位置看到白色.它似乎正在调出 MyCollectionView,但没有显示任何单元格.

Whenever I bring up this view controller I'm getting the navigation bar as expected, but the UICollection view I've added to my nib is nowhere to be seen. All I'm seeing is black where the collection view should be. If I change the background colour of MyCollectionView from default to white, I see white where the collection view ought to be. It seems to be bringing up MyCollectionView, but not showing any cells.

推荐答案

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

If you link your collectionView and its own dataSource and delegate in your xib file, you don't need to set up this on your code.

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

Next, you need to register your UICollectionViewCell :

- (void)viewDidLoad 
{
    [super viewDidLoad];

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

CollectionViewCell_XIB 是你的单元格名称 xibCollectionViewCell_ID 是你的单元格的 ID

CollectionViewCell_XIB is the name of your cell xib CollectionViewCell_ID is the ID of your cell

你需要像这样实现 cellForItemAtIndexPath :

And you need to implement cellForItemAtIndexPath like this :

- (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;
}

这篇关于无法让 UICollectionView 显示单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆