Deprecate indexPath-based Methods on ASTableView/ASCollectionView (#2498)

* Deprecated indexPath methods in ASTableView and ASTableNode and added several indexPath methods to ASTableNode and ASCollectionNode.

* Fixed incorrect doc.
Removed unnecessary __kindof.
This commit is contained in:
george-gw
2016-10-29 18:50:11 +02:00
committed by Adlai Holler
parent c4451edab1
commit d83f058f82
10 changed files with 257 additions and 62 deletions

View File

@@ -375,6 +375,24 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems AS_WARN_UNUSED_RESULT; - (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems AS_WARN_UNUSED_RESULT;
/**
* Retrieve the index path of the item at the given point.
*
* @param point The point of the requested item.
*
* @return The indexPath for the item at the given point. This must be called on the main thread.
*/
- (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point AS_WARN_UNUSED_RESULT;
/**
* Retrieve the cell at the given index path.
*
* @param indexPath The index path of the requested item.
*
* @return The cell for the given index path. This must be called on the main thread.
*/
- (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
/** /**
* Retrieves the context object for the given section, as provided by the data source in * Retrieves the context object for the given section, as provided by the data source in
* the @c collectionNode:contextForSection: method. * the @c collectionNode:contextForSection: method.

View File

@@ -322,17 +322,29 @@
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition - (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) ASCollectionView *collectionView = self.view;
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling selectItemAtIndexPath");
[self.view selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition]; indexPath = [collectionView convertIndexPathFromCollectionNode:indexPath waitingIfNeeded:YES];
if (indexPath != nil) {
[collectionView selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
} else {
NSLog(@"Failed to select item at index path %@ because the item never reached the view.", indexPath);
}
} }
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated - (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) ASCollectionView *collectionView = self.view;
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling deselectItemAtIndexPath");
[self.view deselectItemAtIndexPath:indexPath animated:animated]; indexPath = [collectionView convertIndexPathFromCollectionNode:indexPath waitingIfNeeded:YES];
if (indexPath != nil) {
[collectionView deselectItemAtIndexPath:indexPath animated:animated];
} else {
NSLog(@"Failed to deselect item at index path %@ because the item never reached the view.", indexPath);
}
} }
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
@@ -376,13 +388,20 @@
return self.isNodeLoaded ? [self.view visibleNodes] : @[]; return self.isNodeLoaded ? [self.view visibleNodes] : @[];
} }
- (ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath
{
[self reloadDataInitiallyIfNeeded];
return [self.dataController nodeAtIndexPath:indexPath];
}
- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode - (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode
{ {
return [self.dataController indexPathForNode:cellNode]; return [self.dataController indexPathForNode:cellNode];
} }
- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems - (NSArray<NSIndexPath *> *)indexPathsForVisibleItems
{ {
ASDisplayNodeAssertMainThread();
NSMutableArray *indexPathsArray = [NSMutableArray new]; NSMutableArray *indexPathsArray = [NSMutableArray new];
for (ASCellNode *cell in [self visibleNodes]) { for (ASCellNode *cell in [self visibleNodes]) {
NSIndexPath *indexPath = [self indexPathForNode:cell]; NSIndexPath *indexPath = [self indexPathForNode:cell];
@@ -393,11 +412,28 @@
return indexPathsArray; return indexPathsArray;
} }
- (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point
- (ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath
{ {
[self reloadDataInitiallyIfNeeded]; ASDisplayNodeAssertMainThread();
return [self.dataController nodeAtIndexPath:indexPath]; ASCollectionView *collectionView = self.view;
NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:point];
if (indexPath != nil) {
return [collectionView convertIndexPathToCollectionNode:indexPath];
}
return indexPath;
}
- (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ASDisplayNodeAssertMainThread();
ASCollectionView *collectionView = self.view;
indexPath = [collectionView convertIndexPathFromCollectionNode:indexPath waitingIfNeeded:YES];
if (indexPath == nil) {
return nil;
}
return [collectionView cellForItemAtIndexPath:indexPath];
} }
- (id<ASSectionContext>)contextForSection:(NSInteger)section - (id<ASSectionContext>)contextForSection:(NSInteger)section

View File

@@ -214,6 +214,8 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead."); - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
/** /**
* Perform a batch of updates asynchronously, optionally disabling all animations in the batch. This method must be called from the main thread. * Perform a batch of updates asynchronously, optionally disabling all animations in the batch. This method must be called from the main thread.
* The asyncDataSource must be updated to reflect the changes before the update block completes. * The asyncDataSource must be updated to reflect the changes before the update block completes.

View File

@@ -608,28 +608,6 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
return visibleNodes; return visibleNodes;
} }
/**
* TODO: This method was built when the distinction between data source
* index paths and view index paths was unclear. For compatibility, it
* still expects data source index paths for the time being.
*/
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
{
ASDisplayNodeAssertMainThread();
// If they passed nil, just forward it and be done.
if (indexPath == nil) {
[super selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
return;
}
NSIndexPath *viewIndexPath = [self convertIndexPathFromCollectionNode:indexPath waitingIfNeeded:YES];
if (viewIndexPath != nil) {
[super selectItemAtIndexPath:viewIndexPath animated:animated scrollPosition:scrollPosition];
} else {
NSLog(@"Warning: Ignoring request to select item at index path %@ because the item did not reach the collection view.", indexPath);
}
}
#pragma mark Internal #pragma mark Internal
/** /**

View File

@@ -291,7 +291,6 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
#pragma mark - Querying Data #pragma mark - Querying Data
/** /**
@@ -327,6 +326,74 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (nullable NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode AS_WARN_UNUSED_RESULT; - (nullable NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView rectForRowAtIndexPath:]
*
* @param indexPath An index path identifying a row in the table view.
*
* @return A rectangle defining the area in which the table view draws the row or CGRectZero if indexPath is invalid.
*
* @discussion This method must be called from the main thread.
*/
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView cellForRowAtIndexPath:]
*
* @param indexPath An index path identifying a row in the table view.
*
* @return An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.
*
* @discussion This method must be called from the main thread.
*/
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView indexPathForSelectedRow]
*
* @return The value of this property is an index path identifying the row and section
* indexes of the selected row, or nil if the index path is invalid. If there are multiple selections,
* this property contains the first index-path object in the array of row selections;
* this object has the lowest index values for section and row.
*
* @discussion This method must be called from the main thread.
*/
- (nullable NSIndexPath *)indexPathForSelectedRow AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView indexPathForRowAtPoint:]
*
* @param point A point in the local coordinate system of the table view (the table view€™s bounds).
*
* @return An index path representing the row and section associated with point,
* or nil if the point is out of the bounds of any row.
*
* @discussion This method must be called from the main thread.
*/
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView indexPathsForRowsInRect:]
*
* @param rect A rectangle defining an area of the table view in local coordinates.
*
* @return An array of NSIndexPath objects each representing a row and section index identifying a row within rect.
* Returns an empty array if there arent any rows to return.
*
* @discussion This method must be called from the main thread.
*/
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect AS_WARN_UNUSED_RESULT;
/**
* Similar to -[UITableView indexPathsForVisibleRows]
*
* @return The value of this property is an array of NSIndexPath objects each representing a row index and section index
* that together identify a visible row in the table view. If no rows are visible, the value is nil.
*
* @discussion This method must be called from the main thread.
*/
- (NSArray<NSIndexPath *> *)indexPathsForVisibleRows AS_WARN_UNUSED_RESULT;
@end @end
/** /**

View File

@@ -346,17 +346,28 @@ ASEnvironmentCollectionTableSetEnvironmentState(_environmentStateLock)
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition - (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) ASTableView *tableView = self.view;
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling selectRowAtIndexPath");
[self.view selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition]; indexPath = [tableView convertIndexPathFromTableNode:indexPath waitingIfNeeded:YES];
if (indexPath != nil) {
[tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
} else {
NSLog(@"Failed to select row at index path %@ because the row never reached the view.", indexPath);
}
} }
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) ASTableView *tableView = self.view;
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling deselectRowAtIndexPath");
[self.view deselectRowAtIndexPath:indexPath animated:animated]; indexPath = [tableView convertIndexPathFromTableNode:indexPath waitingIfNeeded:YES];
if (indexPath != nil) {
[tableView deselectRowAtIndexPath:indexPath animated:animated];
} else {
NSLog(@"Failed to deselect row at index path %@ because the row never reached the view.", indexPath);
}
} }
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
@@ -411,6 +422,71 @@ ASEnvironmentCollectionTableSetEnvironmentState(_environmentStateLock)
return [self.dataController nodeAtIndexPath:indexPath]; return [self.dataController nodeAtIndexPath:indexPath];
} }
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASDisplayNodeAssertMainThread();
ASTableView *tableView = self.view;
indexPath = [tableView convertIndexPathFromTableNode:indexPath waitingIfNeeded:YES];
return [tableView rectForRowAtIndexPath:indexPath];
}
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASDisplayNodeAssertMainThread();
ASTableView *tableView = self.view;
indexPath = [tableView convertIndexPathFromTableNode:indexPath waitingIfNeeded:YES];
if (indexPath == nil) {
return nil;
}
return [tableView cellForRowAtIndexPath:indexPath];
}
- (nullable NSIndexPath *)indexPathForSelectedRow
{
ASDisplayNodeAssertMainThread();
ASTableView *tableView = self.view;
NSIndexPath *indexPath = tableView.indexPathForSelectedRow;
if (indexPath != nil) {
return [tableView convertIndexPathToTableNode:indexPath];
}
return indexPath;
}
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
{
ASDisplayNodeAssertMainThread();
ASTableView *tableView = self.view;
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:point];
if (indexPath != nil) {
return [tableView convertIndexPathToTableNode:indexPath];
}
return indexPath;
}
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect
{
ASDisplayNodeAssertMainThread();
ASTableView *tableView = self.view;
return [tableView convertIndexPathsToTableNode:[tableView indexPathsForRowsInRect:rect]];
}
- (NSArray<NSIndexPath *> *)indexPathsForVisibleRows
{
ASDisplayNodeAssertMainThread();
NSMutableArray *indexPathsArray = [NSMutableArray new];
for (ASCellNode *cell in [self visibleNodes]) {
NSIndexPath *indexPath = [self indexPathForNode:cell];
if (indexPath) {
[indexPathsArray addObject:indexPath];
}
}
return indexPathsArray;
}
#pragma mark - Editing #pragma mark - Editing
- (void)reloadDataWithCompletion:(void (^)())completion - (void)reloadDataWithCompletion:(void (^)())completion

View File

@@ -133,6 +133,8 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition; ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
/** /**
* Similar to -visibleCells. * Similar to -visibleCells.
* *

View File

@@ -486,6 +486,23 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
} }
} }
- (NSArray<NSIndexPath *> *)convertIndexPathsToTableNode:(NSArray<NSIndexPath *> *)indexPaths
{
if (indexPaths == nil) {
return nil;
}
NSMutableArray<NSIndexPath *> *indexPathsArray = [NSMutableArray new];
for (NSIndexPath *indexPathInView in indexPaths) {
NSIndexPath *indexPath = [self convertIndexPathToTableNode:indexPathInView];
if (indexPath != nil) {
[indexPathsArray addObject:indexPath];
}
}
return indexPathsArray;
}
- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode - (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode
{ {
return [self indexPathForNode:cellNode waitingIfNeeded:NO]; return [self indexPathForNode:cellNode waitingIfNeeded:NO];
@@ -540,28 +557,6 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
[_dataController waitUntilAllUpdatesAreCommitted]; [_dataController waitUntilAllUpdatesAreCommitted];
} }
/**
* TODO: This method was built when the distinction between data source
* index paths and view index paths was unclear. For compatibility, it
* still expects data source index paths for the time being.
*/
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
{
ASDisplayNodeAssertMainThread();
// If they passed nil, just forward it and be done.
if (indexPath == nil) {
[super selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
return;
}
indexPath = [self convertIndexPathFromTableNode:indexPath waitingIfNeeded:YES];
if (indexPath != nil) {
[super selectRowAtIndexPath:indexPath animated:YES scrollPosition:scrollPosition];
} else {
NSLog(@"Warning: Ignoring request to select row at index path %@ because the item did not reach the table view.", indexPath);
}
}
- (void)layoutSubviews - (void)layoutSubviews
{ {
if (_nodesConstrainedWidth != self.bounds.size.width) { if (_nodesConstrainedWidth != self.bounds.size.width) {

View File

@@ -45,4 +45,18 @@
*/ */
- (NSIndexPath *)convertIndexPathFromTableNode:(NSIndexPath *)indexPath waitingIfNeeded:(BOOL)wait; - (NSIndexPath *)convertIndexPathFromTableNode:(NSIndexPath *)indexPath waitingIfNeeded:(BOOL)wait;
/**
* Attempt to get the node index path given the view-layer index path.
*
* @param indexPath The index path of the row.
*/
- (NSIndexPath *)convertIndexPathToTableNode:(NSIndexPath *)indexPath;
/**
* Attempt to get the node index paths given the view-layer index paths.
*
* @param indexPaths An array of index paths in the view space
*/
- (NSArray<NSIndexPath *> *)convertIndexPathsToTableNode:(NSArray<NSIndexPath *> *)indexPaths;
@end @end

View File

@@ -34,6 +34,13 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (nullable NSIndexPath *)convertIndexPathFromCollectionNode:(NSIndexPath *)indexPath waitingIfNeeded:(BOOL)wait; - (nullable NSIndexPath *)convertIndexPathFromCollectionNode:(NSIndexPath *)indexPath waitingIfNeeded:(BOOL)wait;
/**
* Attempt to get the node index path given the view-layer index path.
*
* @param indexPath The index path of the row.
*/
- (NSIndexPath *)convertIndexPathToCollectionNode:(NSIndexPath *)indexPath;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END