xcode UIView 里面的 UICollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14874320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:46:24  来源:igfitidea点击:

UICollectionView inside UIView

xcodeuiviewcontrollerios6uicollectionview

提问by Matias Frank Jensen

I am trying to implement a UICollectionViewinside a UIView, but I cant find out how to do it. There is a lot of tutorials on how to use UICollectionViewwith a UICollectionViewController, but not how to implement one in a regular View. How do you do that?

我正在尝试在 aUICollectionView内部实现一个UIView,但我不知道如何去做。有关于如何使用大量的教程UICollectionViewUICollectionViewController,但不知道如何实现一个在普通视图。你是怎样做的?

回答by jhilgert00

1)Drag a UICollectionViewinto your UIViewand size it appropriately.

1)将 aUICollectionView拖入您的UIView并适当调整大小。

2)Create a property which is also an IBOutletin your .hfile for the collection view:

2)IBOutlet在您的.h文件中为集合视图创建一个属性:

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

3)Again in your .hfile declare your delegates, so now your .hshould look somethng like this:

3)再次在您的.h文件中声明您的委托,所以现在您.h应该看起来像这样:

@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> {

}

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

4)Connect your myCollectionViewIBOutletin your storyboard.

4)将您myCollectionViewIBOutlet的故事板连接到您的故事板中。

5)(optional) If you're targeting anything older than iOS6 synthesize your myCollectionViewproperty. If you're targeting iOS6, it will auto-synthesize it for you. This goes for all properties, not just UICollectionViews. So in iOS6, you don't need to @synthesize myCollectionView = _myCollectionViewat all. You can just use _mycollectionviewwherever you need to access the property.

5)(可选)如果您的目标是 iOS6 之前的任何内容,请综合您的myCollectionView属性。如果您的目标是 iOS6,它会为您自动合成。这适用于所有属性,而不仅仅是UICollectionViews. 所以在iOS6中,你根本不需要@synthesize myCollectionView = _myCollectionView。您可以_mycollectionview在需要访问该属性的任何地方使用。

6)In your .mfile viewDidLoad, set your delegateand dataSource.

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

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

7)Implement the required dataSource methods:

7)实现所需的 dataSource 方法:

#pragma mark - UICollectionView DataSource 

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

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

From there you can implement as many or as little of the UICollectionViewDelegatemethods as you need. However, 2 are required according to the documentation:

从那里您可以根据需要实现尽可能多或尽可能少的UICollectionViewDelegate方法。但是,根据文档需要 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

It's important to note that you can substitute <UICollectionViewDelegateFlowLayout>for <UICollectionViewDelegate>and still have access to all of the methods in <UICollectionViewDelegate>because <UICollectionViewDelegateFlowLayout>is a subclass of <UICollectionViewDelegate>.

这是要注意的是可以替代的重要<UICollectionViewDelegateFlowLayout><UICollectionViewDelegate>,仍然可以访问所有的方法<UICollectionViewDelegate>,因为<UICollectionViewDelegateFlowLayout>是的子类<UICollectionViewDelegate>

UICollectionViewDataSource Protocol Documentation

UICollectionViewDataSource 协议文档

UICollectionViewDelegate Protocol Documentation

UICollectionViewDelegate 协议文档

回答by Michael Hudson

And the Swift version

和 Swift 版本

  1. Drag a UICollectionView into your UIView and size it appropriately.

  2. Modify your UIViewController to extend UICollectionViewDataSource and UICollectionViewDelegate

  3. Implement the required functions

  4. Control-Drag from your storyboard to the class to create an outlet 'collectionView'

  5. In the viewDidLoad() wire up the delegate and datasource to self collectionView.delegate = selfand collectionView.dataSource = self

  1. 将 UICollectionView 拖入 UIView 并适当调整大小。

  2. 修改您的 UIViewController 以扩展 UICollectionViewDataSource 和 UICollectionViewDelegate

  3. 实现所需的功能

  4. 控制并从故事板拖动到类以创建出口“collectionView”

  5. 在 viewDidLoad() 中将委托和数据源连接到 self collectionView.delegate = selfcollectionView.dataSource = self

It should end up looking like this:

它最终应该是这样的:

    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) {

        }
    }

回答by Hilaj

Drag a UICollectionView into your UIView and size it appropriately. Create a property which is also an IBOutlet in your .h file for the collection view:

将 UICollectionView 拖入 UIView 并适当调整大小。在您的 .h 文件中为集合视图创建一个属性,它也是一个 IBOutlet:

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

in UIView first you need to declare your UICollectionView cell in -(void)awakeFromNib

首先在 UIView 你需要声明你的 UICollectionView 单元格 -(void)awakeFromNib

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

then

然后

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    custom class *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    return cell1;        
}

回答by Tony

  1. Drag uiviewcontroller to the stroryboard.
  2. Create the new class file for the new uiviewcontoller
  3. Set the newly created class to our viewcontroller. And add protocol methods to the .h file. -UICollectionViewDelegate,UICollectionViewDataSource
  4. Drag uicollectionview to the viewcontroller, by default there will be a uicollectionviewcell with the collectionview.
  5. Create new class for the cell customisation as the subclass of the uicollectionviewcell and set the class to the cell in the identity inspector. Also set reuse identifier in the attributes inspector, the name we should specify to identify the uicollectionviewcell. Say cell here.
  6. Drag and drop an imageview(can be anything as your wish) inside the uicollectionviewcell, size it to fit within.
  7. Set an image with the imageview(this image will be shown repeatedly with the collectionview).
  8. Set the delegate and datasource with the uicollectionview to the corresponding viewcontroller.
  9. Set datasource methods - numberOfItemsInSection and cellForItemAtIndexPath.

  10. Return the required cell count with the numberOfItemsInSection method. Say 10 here.

  11. Return the cell to display with the cellForItemAtIndexPath.

    NSString *  identifier = @"cell";
    CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    return cell;
    
  1. 将 uiviewcontroller 拖到故事板。
  2. 为新的 uiviewcontoller 创建新的类文件
  3. 将新创建的类设置为我们的视图控制器。并将协议方法添加到 .h 文件中。-UICollectionViewDelegate,UICollectionViewDataSource
  4. 将 uicollectionview 拖到 viewcontroller 中,默认会有一个带有 collectionview 的 uicollectionviewcell。
  5. 为单元格自定义创建新类作为 uicollectionviewcell 的子类,并将类设置为身份检查器中的单元格。还要在属性检查器中设置重用标识符,我们应该指定该名称来标识 uicollectionviewcell。在这里说单元格。
  6. 在 uicollectionviewcell 中拖放一个图像视图(可以是你想要的任何东西),调整它的大小以适应它。
  7. 使用 imageview 设置图像(此图像将与 collectionview 重复显示)。
  8. 将带有 uicollectionview 的委托和数据源设置为相应的视图控制器。
  9. 设置数据源方法 - numberOfItemsInSection 和 cellForItemAtIndexPath。

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

  11. 使用 cellForItemAtIndexPath 返回要显示的单元格。

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

By now, you should be able to have a visual of a 10 cell collection view :)

到现在为止,您应该能够看到 10 个单元格集合视图:)