Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions AsyncDisplayKit/ASTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions AsyncDisplayKit/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions AsyncDisplayKit/Details/ASDataController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions AsyncDisplayKit/Details/ASDataController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions AsyncDisplayKitTests/ASTableViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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