如何设置UICollectionViewDelegateFlowLayout? [英] How to set 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
。
我承认它可以让你措手不及。
I admit it can catch you 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屋!