Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between UICollectionViewDelegateFlowLayout UICollectionViewFlowLayout

I know that UICollectionViewDelegateFlowLayout is a protocol while UICollectionViewFlowLayout is a class, and I know the difference between a protocol and a class. From my understanding, I can have a class to follow the UICollectionViewDelegateFlowLayout protocol to achieve the exact same effect as having a class inherited from UICollectionViewFlowLayout. I reach this conclusion by finding such relations between the protocol and the class: UICollectionViewDelegateFlowLayout ------------- UICollectionViewFlowLayout collectionView:layout:sizeForItemAtIndexPath:---- itemSize collectionView:layout:insetForSectionAtIndex:---- sectionInset – collectionView:layout:referenceSizeForHeaderInSection: -- headerReferenceSize – collectionView:layout:referenceSizeForFooterInSection: -- footerReferenceSize

I also read the following in the reference of the protocol: "All of the methods in this protocol are optional. If you do not implement a particular method, the flow layout delegate uses values in its own properties for the appropriate spacing information" So my understanding is: if a CollectionView has a layout property and a delegateFlowLayout, the delegateFlowLayout can potentially overwrites something. In other words, I can have both and the protocol will take higher priority. Right?

So, what is the logic behind inventing a protocol and a class that do the same thing?

like image 407
bookmonkie Avatar asked Mar 20 '23 10:03

bookmonkie


1 Answers

The flow layout class is a sort of "default" flow layout which works in many, if not most circumstances. If you need something that UICollectionViewFlowLayout can't handle, then you can subclass UICollectionViewLayout to provide the added functionality.

The delegate methods just provide some limited customization, via the delegate calls, for the default UICollectionViewFlowLayout.

From the docs, "The UICollectionViewDelegateFlowLayout protocol defines methods that let you coordinate with a UICollectionViewFlowLayout object to implement a grid-based layout. The methods of this protocol define the size of items and the spacing between items in the grid".

They don't do the same things; the protocol is provided to allow you to do something extra that the flow layout without the delegate methods doesn't do by default. The protocol, in effect, gives you more "control". You don't have to use the delegate methods if you don't need them.

There is no "priority"; the protocol is optional. So, you don't use the protocol if you don't use UICollectionViewFlowLayout.

like image 92
RegularExpression Avatar answered Apr 07 '23 07:04

RegularExpression