Add support for sectionInset in ASCollectionView in property and delegate form

This commit is contained in:
Levi McCallum 2015-08-03 14:36:29 -07:00
parent 16ed66c3a1
commit 5eb75d6bc7
3 changed files with 27 additions and 2 deletions

View File

@ -320,6 +320,19 @@
*/ */
- (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView; - (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView;
/**
* Passthrough support to UICollectionViewDelegateFlowLayout sectionInset behavior.
*
* @param collectionView The sender.
* @param collectionViewLayout The layout object requesting the information.
* #param section The index number of the section whose insets are needed.
*
* @discussion The same rules apply as the UICollectionView implementation, but this can also be used without a UICollectionViewFlowLayout.
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDelegateFlowLayout/collectionView:layout:insetForSectionAtIndex:
*
*/
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
@end @end
@interface ASCollectionView (Deprecated) @interface ASCollectionView (Deprecated)

View File

@ -118,6 +118,7 @@ static BOOL _isInterceptedSelector(SEL sel)
NSMutableArray *_batchUpdateBlocks; NSMutableArray *_batchUpdateBlocks;
BOOL _asyncDataFetchingEnabled; BOOL _asyncDataFetchingEnabled;
BOOL _implementsInsetSection;
ASBatchContext *_batchContext; ASBatchContext *_batchContext;
} }
@ -165,6 +166,8 @@ static BOOL _isInterceptedSelector(SEL sel)
_performingBatchUpdates = NO; _performingBatchUpdates = NO;
_batchUpdateBlocks = [NSMutableArray array]; _batchUpdateBlocks = [NSMutableArray array];
_implementsInsetSection = ([_asyncDelegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)] ? 1 : 0);
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"_ASCollectionViewCell"]; [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"_ASCollectionViewCell"];
return self; return self;
@ -511,11 +514,16 @@ static BOOL _isInterceptedSelector(SEL sel)
- (CGSize)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath - (CGSize)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{ {
CGSize restrainedSize = self.bounds.size; CGSize restrainedSize = self.bounds.size;
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionViewLayout sectionInset];
if (_implementsInsetSection) {
sectionInset = [_asyncDelegate collectionView:self layout:self.collectionViewLayout insetForSectionAtIndex:indexPath.section];
}
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) { if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
restrainedSize.width = FLT_MAX; restrainedSize.width = MAX(0, FLT_MAX - sectionInset.left - sectionInset.right);
} else { } else {
restrainedSize.height = FLT_MAX; restrainedSize.height = MAX(0, FLT_MAX - sectionInset.top - sectionInset.bottom);
} }
return restrainedSize; return restrainedSize;

View File

@ -95,4 +95,8 @@
[context completeBatchFetching:YES]; [context completeBatchFetching:YES];
} }
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20.0, 20.0, 20.0, 20.0);
}
@end @end