UICollectionView在UIView内部

20

我正在尝试在一个UIView中实现一个UICollectionView,但是我不知道该如何实现。有很多关于如何使用UICollectionViewController来使用UICollectionView的教程,但没有教如何在普通视图中实现一个。
你该如何做到这一点?


你正在使用故事板吗? - jhilgert00
是的,我正在使用故事板。 - Matias Frank Jensen
2
详细答案如下,我只给出一个简单的例子:https://github.com/lequysang/CollectionViewImages - LE SANG
在Swift 3中如何实现这个? - karl71
4个回答

34

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) 在你的storyboard中连接你的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

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

重要的是需要注意,你可以将<UICollectionViewDelegateFlowLayout>用作<UICollectionViewDelegate>的替代品,并且仍然可以访问<UICollectionViewDelegate>中的所有方法,因为<UICollectionViewDelegateFlowLayout><UICollectionViewDelegate>的子类。 UICollectionViewDataSource协议文档 UICollectionViewDelegate协议文档

6
虽然这个回答很有帮助,但它包含了一些错误信息:1)属性的自动合成是一个编译器特性,因此不取决于您所针对的iOS版本;2)将UIViewController子类命名为UtaQuickView是一种不良做法 - 应该将其命名为UtaQuickViewController,因为它不是一个视图;3)UICollectionViewDelegateFlowLayout被说成是“符合”UICollectionViewDelegate,而不是“子类”。抱歉我这么啰嗦...我知道这主要是语义问题,但我认为这很重要。 - Stuart
1
另外,你是否有指向这两个UICollectionViewDelegate方法是必须的文档链接?我正在阅读的文档显示所有方法均为可选的,事实上,UICollectionView头文件也证实了这一点。这是从iOS 6到7改变了吗? - Stuart
4
有关 Swift 的任何答案? - mobibob
也许还可以尝试一下C#/Xamarin的方法? - Chucky
当我调用reload data时,它重新加载整个视图。有什么建议吗? - Waqar Ahmed

3

同时也有Swift版本

  1. 将一个UICollectionView拖入您的UIView中并适当地调整大小。

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

  3. 实现所需的函数

  4. 从storyboard到类进行控制拖动,创建一个outlet 'collectionView'

  5. 在viewDidLoad()中将delegate和datasource连接到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) {

        }
    }

2
将一个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;        
}

1
  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;
    

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接