【问题标题】:UICollectionView inside UIViewUIView里面的UICollectionView
【发布时间】:2013-01-30 05:24:00
【问题描述】:

我正在尝试在UIView 中实现UICollectionView,但我不知道该怎么做。有很多关于如何使用UICollectionViewUICollectionViewController 的教程,但没有如何在常规视图中实现一个。 你是怎么做到的?

【问题讨论】:

标签: xcode uiviewcontroller ios6 uicollectionview


【解决方案1】:

1)UICollectionView 拖入您的UIView 并调整其大小。

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

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

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

@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> {

}

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

4) 在故事板中连接您的 myCollectionView IBOutlet

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

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

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

7) 实现所需的 dataSource 方法:

#pragma mark - UICollectionView DataSource 

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

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

从那里您可以根据需要实现尽可能多或尽可能少的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

请务必注意,您可以将&lt;UICollectionViewDelegateFlowLayout&gt; 替换为&lt;UICollectionViewDelegate&gt;,并且仍然可以访问&lt;UICollectionViewDelegate&gt; 中的所有方法,因为&lt;UICollectionViewDelegateFlowLayout&gt;&lt;UICollectionViewDelegate&gt; 的子类。

UICollectionViewDataSource Protocol Documentation

UICollectionViewDelegate Protocol Documentation

【讨论】:

  • 这个答案很有帮助,但它包含一些错误信息:1)属性的自动合成是编译器功能,因此不依赖于您所针对的 iOS 版本; 2) 命名UIViewController 子类UtaQuickView 是不好的做法——它应该是UtaQuickViewController,因为它不是视图; 3) UICollectionViewDelegateFlowLayout 被称为“符合”UICollectionViewDelegate,它不是“子类”。抱歉这么迂腐……我知道这主要是语义,但我认为这很重要。
  • 另外,您是否有指向说明这两个 UICollectionViewDelegate 方法是必需的文档的链接?我正在阅读的文档显示所有方法都是可选的,实际上UICollectionView 头文件证实了这一点。这是否从 iOS 6 更改为 7?
  • Swift 有什么答案吗?
  • 也许还有一个 C#/Xamarin 方法?
  • 当我调用重新加载数据时,它正在重新加载整个视图。有什么建议怎么办?
【解决方案2】:

还有 Swift 版本

  1. 将一个 UICollectionView 拖到您的 UIView 中并调整其大小。

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

  3. 实现所需功能

  4. 从情节提要中按住 Control 并拖动到类中以创建插座“collectionView”

  5. 在 viewDidLoad() 中将委托和数据源连接到 self 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) {

        }
    }

【讨论】:

    【解决方案3】:

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

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

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

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

    然后

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

    【讨论】:

      【解决方案4】:
      1. 将 uiviewcontroller 拖到故事板中。
      2. 为新的 uiviewcontoller 创建新的类文件
      3. 将新创建的类设置为我们的视图控制器。并将协议方法添加到 .h 文件中。 -UICollectionViewDelegate,UICollectionViewDataSource
      4. 将uicollectionview拖到viewcontroller,默认会有一个uicollectionviewcell和collectionview。
      5. 为单元格自定义创建新类作为 uicollectionviewcell 的子类,并将该类设置为身份检查器中的单元格。还要在属性检查器中设置重用标识符,我们应该指定名称来标识 uicollectionviewcell。在这里说单元格。
      6. 在 uicollectionviewcell 中拖放一个 imageview(可以是任何你想要的任何东西),调整其大小以适合其中。
      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;
        

      现在,您应该可以看到 10 个单元格的集合视图了 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-27
        相关资源
        最近更新 更多