UICollectionView在IB中不包含UICollectionViewCell [英] UICollectionView doesn't contain UICollectionViewCell in IB

查看:100
本文介绍了UICollectionView在IB中不包含UICollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将UICollectionView添加到IB中的.nib文件中。我已经完成了一些教程并没有遇到任何问题。

I'm trying to add a UICollectionView to a .nib file in IB. I've worked through a few tutorials and had no problems.

在我现有的应用程序中,当我将集合视图拖到IB中的视图中然后展开对象时, CollectionView Flow Layout就在那里,但没有集合视图单元格。我也无法将单元格拖放到集合视图中。此外,集合视图的显示方式也不同,显示的是单元格网格的图像,而不是普通的白框。

In my existing app, when I drag a collection view into a view in IB and then expand the object, the CollectionView Flow Layout is there , but there is no collection view cell. I also cannot drag and drop a cell into the collection view. Also the collection view is displayed differently, showing an image of a grid of cells instead of the normal white box.

我尝试使用nib创建一个新的视图控制器,并且在添加collectionView时具有相同的结果。

I've tried creating a new view controller with nib, and have the same results when adding a collectionView.

此应用程序针对iOS 4.2,但我已将部署目标更改为iOS 6。

This app WAS targeting iOS 4.2, but I've changed the deployment target to iOS 6.

在任何新创建的项目中,我都没有这种行为。

In any newly created projects I don't get this behavior.

推荐答案

我可以'告诉你为什么它不起作用,但对我来说,这篇博文中的子类方法解决了这个问题:

I can't tell you why it does not work, but for me the subclass approach from this blog post solved the problem:

http://www.adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial.php

这里有一个简短的总结:

here is a short summary:


  • 创建一个新类MyViewCell,它扩展了UICollectionViewCell并根据需要创建属性,动作,出口和方法。

@interface MyViewCell:UICollectionViewCell


  • 创建一个View(xib文件),其中Collection View Cell作为其根对象(并删除默认创建的对象)。

  • 属性将此Collection View Cell的自定义类设置为扩展类MyViewCell(而不是默认的UICollectionViewCell)。

  • 在Collection Reusable View下的属性中定义标识符,例如myCell,稍后您需要注册您的电话。

  • 返回您的自定义类并修改inithWithFrame方法以加载和使用您创建的视图。

  • create a View (xib file) with a Collection View Cell as its root object (and delete the object which is created by default).
  • in the attributes set the custom class of this Collection View Cell to your extended class MyViewCell (instead of the default UICollectionViewCell).
  • in the attributes under Collection Reusable View define an Identifier, e.g. myCell, you will need this later to register your call.
  • go back to your custom class and modify the inithWithFrame method to load and use your created view.

- (id)initWithFrame:(CGRect)frame 
{ 
   self = [super initWithFrame:frame]; 
   if (self) 
   { 
     // Initialization code 
     NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CVCell" owner:self options:nil];

     if ([arrayOfViews count] < 1) { return nil; }

     if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }

     self = [arrayOfViews objectAtIndex:0];
   }
   return self;
}


  • 然后您可以注册此类以供collectionView使用

  • then you can register this class to be used by the collectionView with

    [self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@myCell] ;

    这篇关于UICollectionView在IB中不包含UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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