Yinghao Wang Yinghao Wang - 6 months ago 23
iOS Question

Why does UICollectionView conform to UICollectionViewDelegateFlowLayout by default?

I'm just confused with the following code that gets executed in a

UICollectionViewController
instantiated from a xib file:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}


which is declared in the protocol
UICollectionViewDelegateFlowLayout
. However, there is nowhere in the
UICollectionViewController
showing that it would conform to this protocol, as declared in the
UICollectionViewController
:

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>


Can anyway explain why
UICollectionViewController
would conform to
UICollectionViewDelegateFlowLayout
?

Answer

By default, the class of a collection view's layout when dragged into a xib/storyboard is UICollectionViewFlowLayout, which is why by default the delegate methods for UICollectionViewDelegateFlowLayout are called. If you were to change the layout object's class in the xib/storyboard, none of the methods specific to UICollectionViewDelegateFlowLayout would be called.

Also, UICollectionViewController may privately conform to UICollectionViewDelegateFlowLayout, or it may not. It doesn't really matter because a class doesn't technically have to explicitly conform to a protocol in order to implement and respond to methods in that protocol.

Comments