From 15aa784fe829666f0b31e29a9439c0eceb620093 Mon Sep 17 00:00:00 2001 From: Hannah Troisi Date: Fri, 21 Oct 2016 10:19:27 -0700 Subject: [PATCH] [ASCollectionNode] add -indexPathsForVisibleNodes (#2451) --- AsyncDisplayKit/ASCollectionNode.h | 6 ++++++ AsyncDisplayKit/ASCollectionNode.mm | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/AsyncDisplayKit/ASCollectionNode.h b/AsyncDisplayKit/ASCollectionNode.h index ab4c06ce03..b36d94ee88 100644 --- a/AsyncDisplayKit/ASCollectionNode.h +++ b/AsyncDisplayKit/ASCollectionNode.h @@ -313,6 +313,12 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode AS_WARN_UNUSED_RESULT; +/** + * Retrieve the index paths of all visible items. + * + * @return an array containing the index paths of all visible items. This must be called on the main thread. + */ +- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems AS_WARN_UNUSED_RESULT; /** * Retrieves the context object for the given section, as provided by the data source in diff --git a/AsyncDisplayKit/ASCollectionNode.mm b/AsyncDisplayKit/ASCollectionNode.mm index 07d2c8c5a4..ab6123be31 100644 --- a/AsyncDisplayKit/ASCollectionNode.mm +++ b/AsyncDisplayKit/ASCollectionNode.mm @@ -295,6 +295,19 @@ return [self.dataController indexPathForNode:cellNode]; } +- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems +{ + NSMutableArray *indexPathsArray = [NSMutableArray new]; + for (ASCellNode *cell in [self visibleNodes]) { + NSIndexPath *indexPath = [self indexPathForNode:cell]; + if (indexPath) { + [indexPathsArray addObject:indexPath]; + } + } + return indexPathsArray; +} + + - (ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath { return [self.dataController nodeAtIndexPath:indexPath];