How to set UICollectionViewDelegateFlowLayout?

The UIViewController maintains a reference to the UICollectionView. The controller must change the inline flow layout using the UICollectionViewDelegateFlowLayout.

It is very easy to configure the presentation data source for yourself:

MyViewController.m

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

But how to set the controller as a representation of a stream of delegates to a view?

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

I tried:

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

But I get an error: "Incompatible pointer types ...".

The collection header file is as follows:

Myviewcontroller.h

 @interface MyViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> 
+90
ios uicollectionview flowlayout
Oct 17
source share
3 answers

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

I admit that he may first catch you on guard.

Oh, and this will only work if self.collectionView.collectionViewLayout actually configured for your stream layout. (or set using initWithFrame:collectionViewLayout:

+230
Oct 17
source share

According to the previous answer, just a usage example. This is really incomprehensible, 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 
+12
Apr 6 '16 at 13:06 on
source share

my two cents for OSX Mojave - Swift

(I got here looking for NSCollectionView ... I know the question was about UICollectionView ..)

All of the above (specifying a delegate also implies cell size) is also suitable for OSX.

NOTE if you write:

Class MyViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {

method:

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

will be called.

When deleting, the delegate method will not be called. (since the class does not obey the protocol).

0
Jul 29 '19 at 10:21
source share



All Articles