Merge pull request #676 from steveram/index-path-for-node

Add a indexPathForNode: to ASTableView
This commit is contained in:
appleguy
2015-09-22 12:13:43 -07:00
5 changed files with 55 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -345,6 +345,11 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
return [_dataController nodeAtIndexPath:indexPath];
}
- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode
{
return [_dataController indexPathForNode:cellNode];
}
- (NSArray *)visibleNodes
{
NSArray *indexPaths = [self indexPathsForVisibleRows];

View File

@@ -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.

View File

@@ -632,6 +632,22 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
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();

View File

@@ -428,4 +428,27 @@
}];
}
- (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