2013-02-14 71 views
18

我想在UIView内实现UICollectionView,但我无法找到如何去做。有很多关于如何使用UICollectionViewUICollectionViewController的教程,但不知道如何在常规视图中实现它。 你是怎么做到的?UIView内的UICollectionView

+0

您使用的一个故事板? – jhilgert00 2013-02-14 13:11:03

+0

是的,我正在使用故事板 – 2013-02-14 13:14:35

+2

详细答案如下,我只是举一个简单的例子:https://github.com/lequysang/CollectionViewImages – 2013-02-16 02:02:25

回答

34

1)UICollectionView拖拽到您的UIView中并适当调整大小。

2)创建一个属性,它也是一个IBOutlet.h文件集合视图:

@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView; 

3)再出现在您.h文件中声明你的代表,所以现在你.h应看起来somethng这样的:

@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> { 

} 

@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView; 

4)在故事板中连接myCollectionViewIBOutlet

5)(可选)如果您定位的任何东西比iOS6都合成myCollectionView属性。如果您的目标是iOS6,它会自动为您合成。这适用于所有房产,而不仅仅是UICollectionViews。所以在iOS6中,根本不需要@synthesize myCollectionView = _myCollectionView。无论您需要访问该属性,您都可以使用_mycollectionview

6)在你.m文件viewDidLoad,设置您的delegatedataSource.

_myCollectionView.delegate = self; 
_myCollectionView.dataSource = self; 

7)实现所需的数据源方法:

#pragma mark - UICollectionView DataSource 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

从那里,你可以实现尽可能多的或者根据需要尽可能少地使用方法。然而,2根据文档要求:

#pragma mark - UICollectionViewDelegate 

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath 

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 

需要注意的是,你可以在<UICollectionViewDelegate><UICollectionViewDelegate>替代<UICollectionViewDelegateFlowLayout>,仍然可以访问所有的方法,因为<UICollectionViewDelegateFlowLayout><UICollectionViewDelegate>一个子类是很重要的。

UICollectionViewDataSource Protocol Documentation

UICollectionViewDelegate Protocol Documentation

+6

尽管这个答案很有帮助,但它包含一些错误信息:1)属性的自动合成是一个编译器功能,因此不依赖于您所定位的iOS版本; 2)命名'UIViewController'子类'UtaQuickView'是不好的做法 - 它应该是'UtaQuickViewController',因为它不是视图; 3)“UICollectionViewDelegateFlowLayout”被认为符合“UICollectionViewDelegate”,它不是“子类”。对不起,如此迂腐......我知道它主要是语义,但我认为这很重要。 – Stuart 2014-01-27 11:09:39

+1

另外,你是否有指向这两个'UICollectionViewDelegate'方法的文档链接?我正在阅读的文档显示所有方法都是可选的,实际上'UICollectionView'头文件证实了这一点。这已从iOS 6更改为7? – Stuart 2014-01-27 11:13:41

+4

Swift的任何答案? – mobibob 2015-09-15 04:53:36

2

拖动一个UICollectionView到你的UIView并适当大小的。 创建一个属性,这也是您的IBOutlet。对于集合视图.h文件:

@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView; 
在UIView的

首先你需要再

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    custom class *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath]; 
    return cell1;   
} 
1
  1. 将UIViewController中声明你的UICollectionView细胞在-(void)awakeFromNib

    [myCollectionView registerNib:[UINib nibWithNibName:@"nib file of collectionView cell" bundle:nil] forCellWithReuseIdentifier:@"identifier"]; 
    

    到stroryboard。

  2. 为新的uiviewcontoller创建新的类文件
  3. 将新创建的类设置为我们的viewcontroller。并将协议方法添加到.h文件。 -UICollectionViewDelegate,UICollectionViewDataSource
  4. 将uicollectionview拖到viewcontroller,默认情况下会有一个带有collectionview的uicollectionviewcell。
  5. 为细胞定制创建新类作为uicollectionviewcell的子类,并将该类设置为身份检查器中的单元。还要在属性检查器中设置重用标识符,我们应该指定该名称来标识uicollectionviewcell。在这里说细胞。
  6. 在uicollectionviewcell中拖放一个imageview(可以是任何你想要的),将其尺寸调整到适合的范围内。
  7. 使用imageview设置图像(该图像将与collectionview重复显示)。
  8. 将具有uicollectionview的委托和数据源设置为相应的viewcontroller。
  9. 设置数据源方法 - numberOfItemsInSection和cellForItemAtIndexPath。

  10. 用numberOfItemsInSection方法返回所需的单元数。在这里说10。

  11. 返回单元格以显示cellForItemAtIndexPath。

    NSString * identifier = @"cell"; 
    CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    return cell; 
    

现在,你应该能够有一个视觉上有10单元集合视图:)

3

而斯威夫特版本

  1. 拖动一个UICollectionView到你的UIView并适当调整大小。

  2. 修改您的UIViewController延长UICollectionViewDataSource和UICollectionViewDelegate

  3. 从故事板实现所要求的功能

  4. 控制-拖动以要创建的类的出口 '的CollectionView'

  5. 在viewDidLoad()将代理和数据源连接到自身 collectionView.delegate = selfcollectionView.dataSource = self

它应该结束这样看:

class CustomerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate 
    { 
     @IBOutlet weak var collectionView: UICollectionView! 

     override func viewDidLoad() { 
      collectionView.delegate = self 
      collectionView.dataSource = self 
     } 

     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     } 

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     } 

     func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 

     } 

     func collectionView(collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, atIndexPath indexPath: NSIndexPath) { 

     } 
    }