无法获取UICollectionView来显示单元格

bee*_*eev 8 ipad ios uicollectionview uicollectionviewcell uicollectionviewlayout

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

我已经创建了UIViewController的子类(带有一个nib),并添加如下:

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];
Run Code Online (Sandbox Code Playgroud)

这个视图控制器是我的UICollectionView的委托和dataSource所以我已经将UICollectionViewDataSource和UICollectionViewDelegate添加到标头中.

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

@property (strong, nonatomic) IBOutlet MyCollectionView *collectionViewController;
Run Code Online (Sandbox Code Playgroud)

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

self.collectionViewController.dataSource = self;
self.collectionViewController.delegate = self;
Run Code Online (Sandbox Code Playgroud)

我还在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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

gup*_*ron 25

另一个值得关注的是,如果在nib或storyboard中设置单元格的标识符,请不要在集合视图控制器中注册nib/class.做一个或另一个,但不是两个.

  • 太棒了,我遇到了同样的问题,这解决了它. (4认同)
  • 这对我来说是个问题.谢谢 (3认同)

Jor*_*tel 21

如果在xib文件中链接collectionView及其自己的dataSource和delegate,则无需在代码上设置它.

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

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Register Nib
    [self.collectionView registerNib:[UINib nibWithNibName:CollectionViewCell_XIB bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CollectionViewCell_ID];
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 欢迎你;)毫不犹豫地加上+1:p (3认同)