如何设置 UICollectionViewDelegateFlowLayout? [英] How to set UICollectionViewDelegateFlowLayout?

查看:39
本文介绍了如何设置 UICollectionViewDelegateFlowLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIViewController 维护对 UICollectionView 的引用.控制器应使用 UICollectionViewDelegateFlowLayout 修改内置流布局.

A UIViewController maintains a reference to a UICollectionView. The controller should modify the built-in flow layout using the UICollectionViewDelegateFlowLayout.

将视图的数据源设置为 self 非常容易:

It's pretty easy to set the view's data source to self:

MyViewController.m

- (void)viewDidLoad
{
    self.collectionView.dataSource = self;
}

但是如何将控制器设置为视图的委托流布局?

But how do I set the controller to be the delegate flow layout of the view?

- (void)viewDidLoad
{
    self.collectionView.dataSource= self;
    // self.collectionView.??? = self; 
}

我试过了:

- (void)viewDidLoad
{
    self.collectionView.dataSource= self;
    self.collectionView.collectionViewLayout = self; 
}

但我得到错误:不兼容的指针类型分配...".

But I get the error: "Incompatible pointer types assigning ...".

集合头文件如下所示:

MyViewController.h

@interface MyViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

推荐答案

只需 self.collectionView.delegate = self;.请注意,UICollectionViewDelegateFlowLayout 继承自 UICollectionViewDelegate.

Just self.collectionView.delegate = self;. Note that UICollectionViewDelegateFlowLayout inherits from UICollectionViewDelegate.

我承认一开始它让我措手不及.

I admit it caught me off guard at first.

哦,这只有在 self.collectionView.collectionViewLayout 实际设置为您的流布局时才有效.(或用 initWithFrame:collectionViewLayout: 设置)

Oh and this will only work if self.collectionView.collectionViewLayout is actually set to your flow layout. (or set with initWithFrame:collectionViewLayout:)

这篇关于如何设置 UICollectionViewDelegateFlowLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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