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];