-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Added selection API to ASTableNode and ASCollectionNode #2453
Changes from 3 commits
c24c633
e544d23
9ad983d
0fcda95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,20 @@ NS_ASSUME_NONNULL_BEGIN | |
*/ | ||
@property (weak, nonatomic) id <ASCollectionDataSource> dataSource; | ||
|
||
/** | ||
* A Boolean value that indicates whether users can select items in the collection node. | ||
* If the value of this property is YES (the default), users can select items. If you want more fine-grained control over the selection of items, you must provide a delegate object and implement the appropriate methods of the UICollectionNodeDelegate protocol. | ||
*/ | ||
@property (nonatomic) BOOL allowsSelection; | ||
|
||
/** | ||
* A Boolean value that determines whether users can select more than one item in the collection node. | ||
* This property controls whether multiple items can be selected simultaneously. The default value of this property is NO. | ||
* When the value of this property is YES, tapping a cell adds it to the current selection (assuming the delegate permits the cell to be selected). Tapping the cell again removes it from the selection. | ||
*/ | ||
@property (nonatomic) BOOL allowsMultipleSelection; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
|
||
|
||
/** | ||
* Tuning parameters for a range type in full mode. | ||
* | ||
|
@@ -272,6 +286,36 @@ NS_ASSUME_NONNULL_BEGIN | |
*/ | ||
- (void)relayoutItems; | ||
|
||
#pragma mark - Selection | ||
|
||
/** | ||
* Selects the item at the specified index path and optionally scrolls it into view. | ||
* If the `allowsSelection` property is NO, calling this method has no effect. If there is an existing selection with a different index path and the `allowsMultipleSelection` property is NO, calling this method replaces the previous selection. | ||
* This method does not cause any selection-related delegate methods to be called. | ||
* | ||
* @param indexPath The index path of the item to select. Specifying nil for this parameter clears the current selection. | ||
* | ||
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it. | ||
* | ||
* @param scrollPosition An option that specifies where the item should be positioned when scrolling finishes. For a list of possible values, see `UICollectionViewScrollPosition`. | ||
* | ||
* @discussion This method must be called from the main thread. | ||
*/ | ||
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition; | ||
|
||
/** | ||
* Deselects the item at the specified index. | ||
* If the allowsSelection property is NO, calling this method has no effect. | ||
* This method does not cause any selection-related delegate methods to be called. | ||
* | ||
* @param indexPath The index path of the item to select. Specifying nil for this parameter clears the current selection. | ||
* | ||
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it. | ||
* | ||
* @discussion This method must be called from the main thread. | ||
*/ | ||
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; | ||
|
||
#pragma mark - Querying Data | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ @interface _ASCollectionPendingState : NSObject | |
@property (weak, nonatomic) id <ASCollectionDelegate> delegate; | ||
@property (weak, nonatomic) id <ASCollectionDataSource> dataSource; | ||
@property (assign, nonatomic) ASLayoutRangeMode rangeMode; | ||
@property (nonatomic) BOOL allowsSelection; // default is YES | ||
@property (nonatomic) BOOL allowsMultipleSelection; // default is NO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
@end | ||
|
||
@implementation _ASCollectionPendingState | ||
|
@@ -37,6 +39,8 @@ - (instancetype)init | |
self = [super init]; | ||
if (self) { | ||
_rangeMode = ASLayoutRangeModeCount; | ||
_allowsSelection = YES; | ||
_allowsMultipleSelection = NO; | ||
} | ||
return self; | ||
} | ||
|
@@ -143,9 +147,11 @@ - (void)didLoad | |
|
||
if (_pendingState) { | ||
_ASCollectionPendingState *pendingState = _pendingState; | ||
self.pendingState = nil; | ||
view.asyncDelegate = pendingState.delegate; | ||
view.asyncDataSource = pendingState.dataSource; | ||
self.pendingState = nil; | ||
view.asyncDelegate = pendingState.delegate; | ||
view.asyncDataSource = pendingState.dataSource; | ||
view.allowsSelection = pendingState.allowsSelection; | ||
view.allowsMultipleSelection = pendingState.allowsMultipleSelection; | ||
|
||
if (pendingState.rangeMode != ASLayoutRangeModeCount) { | ||
[view.rangeController updateCurrentRangeWithMode:pendingState.rangeMode]; | ||
|
@@ -251,6 +257,44 @@ - (void)setDataSource:(id <ASCollectionDataSource>)dataSource | |
} | ||
} | ||
|
||
- (void)setAllowsSelection:(BOOL)allowsSelection | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsSelection = allowsSelection; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsSelection = allowsSelection; | ||
} | ||
} | ||
|
||
- (BOOL)allowsSelection | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsSelection; | ||
} else { | ||
return self.view.allowsSelection; | ||
} | ||
} | ||
|
||
- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsMultipleSelection = allowsMultipleSelection; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsMultipleSelection = allowsMultipleSelection; | ||
} | ||
} | ||
|
||
- (BOOL)allowsMultipleSelection | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsMultipleSelection; | ||
} else { | ||
return self.view.allowsMultipleSelection; | ||
} | ||
} | ||
|
||
#pragma mark - Range Tuning | ||
|
||
- (ASRangeTuningParameters)tuningParametersForRangeType:(ASLayoutRangeType)rangeType | ||
|
@@ -273,6 +317,24 @@ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMo | |
return [self.rangeController setTuningParameters:tuningParameters forRangeMode:rangeMode rangeType:rangeType]; | ||
} | ||
|
||
#pragma mark - Selection | ||
|
||
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling selectItemAtIndexPath"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future it will be nice to remove this restriction. It would require that we have a clean method for ensuring that the view is loaded, that the initial reload has been called, and that all updates are committed. Today we could cobble together all that into a method but it would be ugly and there are other potential hazards (what if we're inside Also, would you be willing to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, I added a todo referring to your discussion post. I added the |
||
[self.view selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition]; | ||
} | ||
|
||
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling deselectItemAtIndexPath"); | ||
[self.view deselectItemAtIndexPath:indexPath animated:animated]; | ||
} | ||
|
||
#pragma mark - Querying Data | ||
|
||
- (NSInteger)numberOfItemsInSection:(NSInteger)section | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,27 @@ NS_ASSUME_NONNULL_BEGIN | |
@property (weak, nonatomic) id <ASTableDelegate> delegate; | ||
@property (weak, nonatomic) id <ASTableDataSource> dataSource; | ||
|
||
/* | ||
* A Boolean value that determines whether users can select a row. | ||
* If the value of this property is YES (the default), users can select rows. If you set it to NO, they cannot select rows. Setting this property affects cell selection only when the table view is not in editing mode. If you want to restrict selection of cells in editing mode, use `allowsSelectionDuringEditing`. | ||
*/ | ||
@property (nonatomic) BOOL allowsSelection; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
/* | ||
* A Boolean value that determines whether users can select cells while the table view is in editing mode. | ||
* If the value of this property is YES, users can select rows during editing. The default value is NO. If you want to restrict selection of cells regardless of mode, use allowsSelection. | ||
*/ | ||
@property (nonatomic) BOOL allowsSelectionDuringEditing; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
/* | ||
* A Boolean value that determines whether users can select more than one row outside of editing mode. | ||
* This property controls whether multiple rows can be selected simultaneously outside of editing mode. When the value of this property is YES, each row that is tapped acquires a selected appearance. Tapping the row again removes the selected appearance. If you access indexPathsForSelectedRows, you can get the index paths that identify the selected rows. | ||
*/ | ||
@property (nonatomic) BOOL allowsMultipleSelection; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
/* | ||
* A Boolean value that controls whether users can select more than one cell simultaneously in editing mode. | ||
* The default value of this property is NO. If you set it to YES, check marks appear next to selected rows in editing mode. In addition, UITableView does not query for editing styles when it goes into editing mode. If you access indexPathsForSelectedRows, you can get the index paths that identify the selected rows. | ||
*/ | ||
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
|
||
/** | ||
* Tuning parameters for a range type in full mode. | ||
* | ||
|
@@ -230,6 +251,38 @@ NS_ASSUME_NONNULL_BEGIN | |
*/ | ||
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath; | ||
|
||
#pragma mark - Selection | ||
|
||
/** | ||
* Selects a row in the table view identified by index path, optionally scrolling the row to a location in the table view. | ||
* This method does not cause any selection-related delegate methods to be called. | ||
* | ||
* @param indexPath An index path identifying a row in the table view. | ||
* | ||
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it. | ||
* | ||
* @param scrollPosition A constant that identifies a relative position in the table view (top, middle, bottom) for the row when scrolling concludes. See `UITableViewScrollPosition` for descriptions of valid constants. | ||
* | ||
* @discussion This method must be called from the main thread. | ||
*/ | ||
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition; | ||
|
||
/* | ||
* Deselects a given row identified by index path, with an option to animate the deselection. | ||
* This method does not cause any selection-related delegate methods to be called. | ||
* Calling this method does not cause any scrolling to the deselected row. | ||
* | ||
* @param indexPath An index path identifying a row in the table view. | ||
* | ||
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it. | ||
* | ||
* @discussion This method must be called from the main thread. | ||
*/ | ||
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; | ||
|
||
|
||
#pragma mark - Querying Data | ||
|
||
/** | ||
* Retrieves the number of rows in the given section. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,10 @@ @interface _ASTablePendingState : NSObject | |
@property (weak, nonatomic) id <ASTableDelegate> delegate; | ||
@property (weak, nonatomic) id <ASTableDataSource> dataSource; | ||
@property (assign, nonatomic) ASLayoutRangeMode rangeMode; | ||
@property (nonatomic) BOOL allowsSelection; | ||
@property (nonatomic) BOOL allowsSelectionDuringEditing; | ||
@property (nonatomic) BOOL allowsMultipleSelection; | ||
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add |
||
@end | ||
|
||
@implementation _ASTablePendingState | ||
|
@@ -33,6 +37,10 @@ - (instancetype)init | |
self = [super init]; | ||
if (self) { | ||
_rangeMode = ASLayoutRangeModeCount; | ||
_allowsSelection = YES; | ||
_allowsSelectionDuringEditing = NO; | ||
_allowsMultipleSelection = NO; | ||
_allowsMultipleSelectionDuringEditing = NO; | ||
} | ||
return self; | ||
} | ||
|
@@ -104,6 +112,10 @@ - (void)didLoad | |
self.pendingState = nil; | ||
view.asyncDelegate = pendingState.delegate; | ||
view.asyncDataSource = pendingState.dataSource; | ||
view.allowsSelection = pendingState.allowsSelection; | ||
view.allowsSelectionDuringEditing = pendingState.allowsSelectionDuringEditing; | ||
view.allowsMultipleSelection = pendingState.allowsMultipleSelection; | ||
view.allowsMultipleSelectionDuringEditing = pendingState.allowsMultipleSelectionDuringEditing; | ||
if (pendingState.rangeMode != ASLayoutRangeModeCount) { | ||
[view.rangeController updateCurrentRangeWithMode:pendingState.rangeMode]; | ||
} | ||
|
@@ -215,6 +227,82 @@ - (void)setDataSource:(id <ASTableDataSource>)dataSource | |
} | ||
} | ||
|
||
- (void)setAllowsSelection:(BOOL)allowsSelection | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsSelection = allowsSelection; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsSelection = allowsSelection; | ||
} | ||
} | ||
|
||
- (BOOL)allowsSelection | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsSelection; | ||
} else { | ||
return self.view.allowsSelection; | ||
} | ||
} | ||
|
||
- (void)setAllowsSelectionDuringEditing:(BOOL)allowsSelectionDuringEditing | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsSelectionDuringEditing = allowsSelectionDuringEditing; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsSelectionDuringEditing = allowsSelectionDuringEditing; | ||
} | ||
} | ||
|
||
- (BOOL)allowsSelectionDuringEditing | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsSelectionDuringEditing; | ||
} else { | ||
return self.view.allowsSelectionDuringEditing; | ||
} | ||
} | ||
|
||
- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsMultipleSelection = allowsMultipleSelection; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsMultipleSelection = allowsMultipleSelection; | ||
} | ||
} | ||
|
||
- (BOOL)allowsMultipleSelection | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsMultipleSelection; | ||
} else { | ||
return self.view.allowsMultipleSelection; | ||
} | ||
} | ||
|
||
- (void)setAllowsMultipleSelectionDuringEditing:(BOOL)allowsMultipleSelectionDuringEditing | ||
{ | ||
if ([self pendingState]) { | ||
_pendingState.allowsMultipleSelectionDuringEditing = allowsMultipleSelectionDuringEditing; | ||
} else { | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist"); | ||
self.view.allowsMultipleSelectionDuringEditing = allowsMultipleSelectionDuringEditing; | ||
} | ||
} | ||
|
||
- (BOOL)allowsMultipleSelectionDuringEditing | ||
{ | ||
if ([self pendingState]) { | ||
return _pendingState.allowsMultipleSelectionDuringEditing; | ||
} else { | ||
return self.view.allowsMultipleSelectionDuringEditing; | ||
} | ||
} | ||
|
||
#pragma mark ASRangeControllerUpdateRangeProtocol | ||
|
||
- (void)updateCurrentRangeWithMode:(ASLayoutRangeMode)rangeMode | ||
|
@@ -253,8 +341,25 @@ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMo | |
return [self.rangeController setTuningParameters:tuningParameters forRangeMode:rangeMode rangeType:rangeType]; | ||
} | ||
|
||
#pragma mark - Querying Data | ||
#pragma mark - Selection | ||
|
||
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling selectRowAtIndexPath"); | ||
[self.view selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition]; | ||
} | ||
|
||
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457) | ||
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling deselectRowAtIndexPath"); | ||
[self.view deselectRowAtIndexPath:indexPath animated:animated]; | ||
} | ||
|
||
#pragma mark - Querying Data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add white space below |
||
- (NSInteger)numberOfRowsInSection:(NSInteger)section | ||
{ | ||
return [self.dataController numberOfRowsInSection:section]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
assign
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All done, I took the liberty to change the order of (nonatomic and assign) of two other properties to keep the consistency with the rest of the framework.