From d09888f9c235b502143042c9b017a214cabb1fa3 Mon Sep 17 00:00:00 2001 From: Steven Ramkumar Date: Tue, 22 Sep 2015 11:16:55 -0700 Subject: [PATCH] Add a indexPathForNode: to ASTableView --- AsyncDisplayKit/ASTableView.h | 9 ++++++++ AsyncDisplayKit/ASTableView.mm | 5 +++++ AsyncDisplayKit/Details/ASDataController.h | 2 ++ AsyncDisplayKit/Details/ASDataController.mm | 16 ++++++++++++++ AsyncDisplayKitTests/ASTableViewTests.m | 23 +++++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/AsyncDisplayKit/ASTableView.h b/AsyncDisplayKit/ASTableView.h index ed3ecd6212..c562f83f2f 100644 --- a/AsyncDisplayKit/ASTableView.h +++ b/AsyncDisplayKit/ASTableView.h @@ -227,6 +227,15 @@ */ - (ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath; +/** + * Similar to -indexPathForCell:. + * + * @param cellNode a cellNode part of the table view + * + * @returns an indexPath for this cellNode + */ +- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode; + /** * Similar to -visibleCells. * diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index b7ebe9c2f1..47ed7778ac 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -345,6 +345,11 @@ - (ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath return [_dataController nodeAtIndexPath:indexPath]; } +- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode +{ + return [_dataController indexPathForNode:cellNode]; +} + - (NSArray *)visibleNodes { NSArray *indexPaths = [self indexPathsForVisibleRows]; diff --git a/AsyncDisplayKit/Details/ASDataController.h b/AsyncDisplayKit/Details/ASDataController.h index 68e586daa8..589d210f9d 100644 --- a/AsyncDisplayKit/Details/ASDataController.h +++ b/AsyncDisplayKit/Details/ASDataController.h @@ -173,6 +173,8 @@ typedef NSUInteger ASDataControllerAnimationOptions; - (ASCellNode *)nodeAtIndexPath:(NSIndexPath *)indexPath; +- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode; + - (NSArray *)nodesAtIndexPaths:(NSArray *)indexPaths; - (NSArray *)completedNodes; // This provides efficient access to the entire _completedNodes multidimensional array. diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index b0e7a94bb5..3b80baecd0 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -632,6 +632,22 @@ - (ASCellNode *)nodeAtIndexPath:(NSIndexPath *)indexPath return _completedNodes[indexPath.section][indexPath.row]; } +- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode; +{ + ASDisplayNodeAssertMainThread(); + + // Loop through each section to look for the cellNode + for (NSUInteger i = 0; i < [_completedNodes count]; i++) { + NSArray *sectionNodes = _completedNodes[i]; + NSUInteger cellIndex = [sectionNodes indexOfObjectIdenticalTo:cellNode]; + if (cellIndex != NSNotFound) { + return [NSIndexPath indexPathForRow:cellIndex inSection:i]; + } + } + + return nil; +} + - (NSArray *)nodesAtIndexPaths:(NSArray *)indexPaths { ASDisplayNodeAssertMainThread(); diff --git a/AsyncDisplayKitTests/ASTableViewTests.m b/AsyncDisplayKitTests/ASTableViewTests.m index bdba88cf19..a0d2a413b7 100644 --- a/AsyncDisplayKitTests/ASTableViewTests.m +++ b/AsyncDisplayKitTests/ASTableViewTests.m @@ -428,4 +428,27 @@ - (void)DISABLED_testRelayoutRowsAfterEditingModeIsChangedAndTheyBecomeVisible }]; } +- (void)testIndexPathForNode +{ + CGSize tableViewSize = CGSizeMake(100, 500); + ASTestTableView *tableView = [[ASTestTableView alloc] initWithFrame:CGRectMake(0, 0, tableViewSize.width, tableViewSize.height) + style:UITableViewStylePlain + asyncDataFetching:YES]; + ASTableViewFilledDataSource *dataSource = [ASTableViewFilledDataSource new]; + + tableView.asyncDelegate = dataSource; + tableView.asyncDataSource = dataSource; + + [tableView reloadDataWithCompletion:^{ + for (NSUInteger i = 0; i < NumberOfSections; i++) { + for (NSUInteger j = 0; j < NumberOfRowsPerSection; j++) { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i]; + ASCellNode *cellNode = [tableView nodeForRowAtIndexPath:indexPath]; + NSIndexPath *reportedIndexPath = [tableView indexPathForNode:cellNode]; + XCTAssertEqual(indexPath.row, reportedIndexPath.row); + } + } + }]; +} + @end