ios 如何设置 UICollectionViewDelegateFlowLayout?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12925280/
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-08-30 20:47:22  来源:igfitidea点击:

How to set UICollectionViewDelegateFlowLayout?

iosuicollectionviewflowlayout

提问by Rich Apodaca

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

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

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

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

MyViewController.m

视图控制器.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; 
}

I've tried:

我试过了:

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

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

但我收到错误:“不兼容的指针类型分配......”。

The collection header file looks like this:

集合头文件如下所示:

MyViewController.h

视图控制器.h

@interface MyViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

回答by John Estropia

Just self.collectionView.delegate = self;. Note that UICollectionViewDelegateFlowLayoutinherits from UICollectionViewDelegate.

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

I admit it can catch you off guard at first.

我承认它一开始会让你措手不及。

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

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

回答by WINSergey

According to previous answer just example of use. It really not clear but I can show how it works:

根据之前的回答只是使用示例。这真的不清楚,但我可以展示它是如何工作的:

@interface PrettyViewController()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
    //some code
@end

@implementation PrettyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.collectionView.delegate = self;//bingo! right here
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake([[UIScreen mainScreen] bounds].size.width, 20.0);
}


@end

回答by ingconti

my two cents for OSX mojave - Swift

我的OSX mojave两分钱- Swift

(I've fallen here searching for NSCollectionView... I do know question was about UICollectionView..)

(我在这里寻找 NSCollectionView ......我知道问题是关于 UICollectionView ......)

All said above (specifying delegate implies cell size, too) is correct for OSX, too.

上面所说的(指定委托也意味着单元格大小)对于 OSX 也是正确的。

NOTEif You write:

注意如果你写:

class MyViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout{

类 MyViewController:NSCollectionViewDelegate、NSCollectionViewDataSource、 NSCollectionViewDelegateFlowLayout{

the method:

方法:

func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize

func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize

willbe called.

被调用。

IF removed, no delegate method will be called. (as class does not obey to the protocol).

如果删除,则不会调用任何委托方法。(因为班级不遵守协议)。