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

查看:100
本文介绍了无法获取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的子类(带有一个nib),并添加如下:

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的委托和dataSource所以我添加了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.

推荐答案

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

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 是您的单元格名称xib
CollectionViewCell_ID 是您单元格的ID

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

您需要实现 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;
}

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

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