diff --git a/JNWCollectionView.podspec b/JNWCollectionView.podspec index c15c51f..1a987e8 100644 --- a/JNWCollectionView.podspec +++ b/JNWCollectionView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "JNWCollectionView" - s.version = "1.2" + s.version = "1.3" s.summary = "A highly customizable and performant collection view for the Mac." s.homepage = "https://github.com/jwilling/JNWCollectionView" s.screenshots = "http://jwilling.com/drop/github/JNWCollectionView.png" diff --git a/JNWCollectionView.xcodeproj/project.pbxproj b/JNWCollectionView.xcodeproj/project.pbxproj index d87ac59..b56a6a2 100644 --- a/JNWCollectionView.xcodeproj/project.pbxproj +++ b/JNWCollectionView.xcodeproj/project.pbxproj @@ -446,7 +446,7 @@ ABB023E8170791D300537A92 /* Project object */ = { isa = PBXProject; attributes = { - LastTestingUpgradeCheck = 0510; + LastTestingUpgradeCheck = 0610; LastUpgradeCheck = 0500; ORGANIZATIONNAME = AppJon; }; diff --git a/JNWCollectionView/JNWCollectionViewFramework.h b/JNWCollectionView/JNWCollectionViewFramework.h index 6f28e57..36a84b6 100644 --- a/JNWCollectionView/JNWCollectionViewFramework.h +++ b/JNWCollectionView/JNWCollectionViewFramework.h @@ -108,6 +108,9 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) { /// Tells the delegate that the item at the specified index path has been right-clicked. - (void)collectionView:(JNWCollectionView *)collectionView didRightClickItemAtIndexPath:(NSIndexPath *)indexPath; +/// Asks the delegate if the item at the specified index path should be scrolled to. +- (BOOL)collectionView:(JNWCollectionView *)collectionView shouldScrollToItemAtIndexPath:(NSIndexPath *)indexPath; + /// Tells the delegate that the specified index path has been scrolled to. - (void)collectionView:(JNWCollectionView *)collectionView didScrollToItemAtIndexPath:(NSIndexPath *)indexPath; @@ -115,6 +118,12 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) { /// back into the reuse queue. - (void)collectionView:(JNWCollectionView *)collectionView didEndDisplayingCell:(JNWCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath; +/// Tells the delegate that supplementary view will be added to document view. +- (void)collectionView:(JNWCollectionView *)collectionView willDisplaySupplementaryView:(JNWCollectionViewReusableView *)supplementaryView ofKind:(NSString *)kind forsection:(NSInteger)section; + +/// Tells the delegate that supplementary view was removed from document view. +- (void)collectionView:(JNWCollectionView *)collectionView didEndDisplayingSupplementaryView:(JNWCollectionViewReusableView *)supplementaryView ofKind:(NSString *)kind forsection:(NSInteger)section; + @end #pragma mark Reloading and customizing @@ -235,6 +244,12 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) { /// previously registered in -registerClass:forSupplementaryViewOfKind:reuseIdentifier:. - (JNWCollectionViewReusableView *)supplementaryViewForKind:(NSString *)kind reuseIdentifier:(NSString *)reuseIdentifier inSection:(NSInteger)section; +/// Returns an array of all the currently visible supplementary views. The cells are not guaranteed to be in any order. +- (NSArray *)visibleSupplementaryViews; + +/// Returns an array of all the currently visible supplementary views for the specified kind. The cells are not guaranteed to be in any order. +- (NSArray *)visibleSupplementaryViewsForKind:(NSString *)kind; + /// Returns an array of all the currently visible cells. The cells are not guaranteed to be in any order. - (NSArray *)visibleCells; diff --git a/JNWCollectionView/JNWCollectionViewFramework.m b/JNWCollectionView/JNWCollectionViewFramework.m index b046b03..f0074ba 100644 --- a/JNWCollectionView/JNWCollectionViewFramework.m +++ b/JNWCollectionView/JNWCollectionViewFramework.m @@ -45,10 +45,13 @@ @interface JNWCollectionView() { unsigned int delegateDidSelect:1; unsigned int delegateShouldDeselect:1; unsigned int delegateDidDeselect:1; + unsigned int delegateShouldScroll:1; unsigned int delegateDidScroll:1; unsigned int delegateDidDoubleClick:1; unsigned int delegateDidRightClick:1; unsigned int delegateDidEndDisplayingCell:1; + unsigned int delegateWillDisplaySupplementaryView:1; + unsigned int delegateDidEndDisplayingSupplementaryView:1; unsigned int wantsLayout; } _collectionViewFlags; @@ -74,6 +77,8 @@ @interface JNWCollectionView() { @property (nonatomic, strong) NSMutableDictionary *supplementaryViewClassMap; // { "kind/identifier" : class } @property (nonatomic, strong) NSMutableDictionary *supplementaryViewNibMap; // { "kind/identifier" : nib } +@property (nonatomic, strong) NSView *documentView; + @end @implementation JNWCollectionView @@ -138,13 +143,16 @@ - (void)setDelegate:(id)delegate { _collectionViewFlags.delegateDidDeselect = [delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]; _collectionViewFlags.delegateDidDoubleClick = [delegate respondsToSelector:@selector(collectionView:didDoubleClickItemAtIndexPath:)]; _collectionViewFlags.delegateDidRightClick = [delegate respondsToSelector:@selector(collectionView:didRightClickItemAtIndexPath:)]; - _collectionViewFlags.delegateDidEndDisplayingCell = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingCell:forItemAtIndexPath:)]; + _collectionViewFlags.delegateDidEndDisplayingCell = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingCell:forItemAtIndexPath:)]; + _collectionViewFlags.delegateShouldScroll = [delegate respondsToSelector:@selector(collectionView:shouldScrollToItemAtIndexPath:)]; + _collectionViewFlags.delegateDidScroll = [delegate respondsToSelector:@selector(collectionView:didScrollToItemAtIndexPath:)]; + _collectionViewFlags.delegateWillDisplaySupplementaryView = [delegate respondsToSelector:@selector(collectionView:willDisplaySupplementaryView:ofKind:forsection:)]; + _collectionViewFlags.delegateDidEndDisplayingSupplementaryView = [delegate respondsToSelector:@selector(collectionView:didEndDisplayingSupplementaryView:ofKind:forsection:)]; } - (void)setDataSource:(id)dataSource { _dataSource = dataSource; _collectionViewFlags.dataSourceNumberOfSections = [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]; - _collectionViewFlags.delegateDidScroll = [dataSource respondsToSelector:@selector(collectionView:didScrollToItemAtIndexPath:)]; _collectionViewFlags.dataSourceViewForSupplementaryView = [dataSource respondsToSelector:@selector(collectionView:viewForSupplementaryViewOfKind:inSection:)]; NSAssert(dataSource == nil || [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)], @"data source must implement collectionView:numberOfItemsInSection"); @@ -354,6 +362,16 @@ - (void)resetAllCellsAndSupplementaryViews { } } [self.visibleCellsMap removeAllObjects]; + + if (_collectionViewFlags.delegateDidEndDisplayingSupplementaryView) { + [self.visibleSupplementaryViewsMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + + NSInteger section = [self sectionForSupplementaryLayoutIdentifier:key]; + JNWCollectionViewReusableView *supplementaryView = obj; + [self.delegate collectionView:self didEndDisplayingSupplementaryView:supplementaryView ofKind:supplementaryView.kind forsection:section]; + + }]; + } [self.visibleSupplementaryViewsMap removeAllObjects]; // Remove any cells or views that might be added to the document view. @@ -394,6 +412,23 @@ - (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point { return nil; } +- (NSArray *)visibleSupplementaryViews { + return self.visibleSupplementaryViewsMap.allValues; +} + +- (NSArray *)visibleSupplementaryViewsForKind:(NSString *)kind { + NSArray *allKeys = self.visibleSupplementaryViewsMap.allKeys; + NSMutableArray *visibleSupplementaryViews = [[NSMutableArray alloc] initWithCapacity:allKeys.count]; + + for (NSString *key in allKeys) { + NSString *supplementaryViewKind = [self kindForSupplementaryViewIdentifier:key]; + if ([kind isEqualToString:supplementaryViewKind]) { + [visibleSupplementaryViews addObject:self.visibleSupplementaryViewsMap[key]]; + } + } + return visibleSupplementaryViews; +} + - (NSArray *)visibleCells { return self.visibleCellsMap.allValues; } @@ -486,6 +521,10 @@ - (NSArray *)indexPathsForVisibleItems { } - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(JNWCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated { + if (_collectionViewFlags.delegateShouldScroll && ![self.delegate collectionView:self shouldScrollToItemAtIndexPath:indexPath]) { + return; + } + CGRect rect = [self rectForItemAtIndexPath:indexPath]; CGRect visibleRect = self.documentVisibleRect; @@ -802,6 +841,10 @@ - (void)layoutSupplementaryViewsWithRedraw:(BOOL)needsVisibleRedraw { [view removeFromSuperview]; [self enqueueReusableSupplementaryView:view ofKind:view.kind withReuseIdentifier:view.reuseIdentifier]; + + if (_collectionViewFlags.delegateDidEndDisplayingSupplementaryView) { + [self.delegate collectionView:self didEndDisplayingSupplementaryView:view ofKind:view.kind forsection:[self sectionForSupplementaryLayoutIdentifier:layoutIdentifier]]; + } } // Add new views @@ -817,6 +860,11 @@ - (void)layoutSupplementaryViewsWithRedraw:(BOOL)needsVisibleRedraw { JNWCollectionViewLayoutAttributes *attributes = [self.collectionViewLayout layoutAttributesForSupplementaryItemInSection:section kind:kind]; view.frame = attributes.frame; view.alphaValue = attributes.alpha; + + if (_collectionViewFlags.delegateWillDisplaySupplementaryView) { + [self.delegate collectionView:self willDisplaySupplementaryView:view ofKind:view.kind forsection:[self sectionForSupplementaryLayoutIdentifier:layoutIdentifier]]; + } + [self.documentView addSubview:view]; self.visibleSupplementaryViewsMap[layoutIdentifier] = view; @@ -940,7 +988,7 @@ - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath selectionType:(JNWCollectionViewSelectionType)selectionType { if (indexPath == nil) return; - + NSMutableSet *indexesToSelect = [NSMutableSet set]; if (selectionType == JNWCollectionViewSelectionTypeSingle) { diff --git a/JNWCollectionView/NSIndexPath+JNWAdditions.m b/JNWCollectionView/NSIndexPath+JNWAdditions.m index 2b89727..f1af89c 100644 --- a/JNWCollectionView/NSIndexPath+JNWAdditions.m +++ b/JNWCollectionView/NSIndexPath+JNWAdditions.m @@ -35,18 +35,4 @@ - (NSInteger)jnw_item { return [self indexAtPosition:1]; } -- (BOOL)isEqual:(id)object { - if ([object isKindOfClass:NSIndexPath.class]) { - if (self.jnw_section == [(NSIndexPath *)object jnw_section] && self.jnw_item == [(NSIndexPath *)object jnw_item]) { - return YES; - } - } - - return NO; -} - -- (NSString *)debugDescription { - return [NSString stringWithFormat:@"<%@: %p; section = %ld; item = %ld>", self.class, self, self.jnw_section, self.jnw_item]; -} - @end