Skip to content

Commit e6b33dd

Browse files
author
Levi McCallum
committed
Fix cell selection and highlighting in ASCollectionView
1 parent 5b1e214 commit e6b33dd

5 files changed

Lines changed: 85 additions & 9 deletions

File tree

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,22 @@ @interface _ASCollectionViewCell : UICollectionViewCell
115115

116116
@implementation _ASCollectionViewCell
117117

118+
- (void)setNode:(ASCellNode *)node
119+
{
120+
_node = node;
121+
node.selected = self.selected;
122+
node.highlighted = self.highlighted;
123+
}
124+
118125
- (void)setSelected:(BOOL)selected
119126
{
127+
[super setSelected:selected];
120128
_node.selected = selected;
121129
}
122130

123131
- (void)setHighlighted:(BOOL)highlighted
124132
{
133+
[super setHighlighted:highlighted];
125134
_node.highlighted = highlighted;
126135
}
127136

@@ -478,11 +487,8 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
478487
_ASCollectionViewCell *cell = [self dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
479488

480489
ASCellNode *node = [_dataController nodeAtIndexPath:indexPath];
481-
482-
[_rangeController configureContentView:cell.contentView forCellNode:node];
483-
484490
cell.node = node;
485-
491+
[_rangeController configureContentView:cell.contentView forCellNode:node];
486492
return cell;
487493
}
488494

examples/ASCollectionView/Sample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
25FDEC921BF31EE700CEB123 /* ItemNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 25FDEC911BF31EE700CEB123 /* ItemNode.m */; };
1011
9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */; };
1112
9BA2CEA11BB2579C00D18414 /* Launchboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */; };
1213
AC3C4A641A11F47200143C57 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A631A11F47200143C57 /* main.m */; };
@@ -17,6 +18,8 @@
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXFileReference section */
21+
25FDEC901BF31EE700CEB123 /* ItemNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ItemNode.h; sourceTree = "<group>"; };
22+
25FDEC911BF31EE700CEB123 /* ItemNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ItemNode.m; sourceTree = "<group>"; };
2023
2DBAEE96397BB913350C4530 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
2124
9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SupplementaryNode.h; sourceTree = "<group>"; };
2225
9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SupplementaryNode.m; sourceTree = "<group>"; };
@@ -83,6 +86,8 @@
8386
AC3C4A611A11F47200143C57 /* Supporting Files */,
8487
9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */,
8588
9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */,
89+
25FDEC901BF31EE700CEB123 /* ItemNode.h */,
90+
25FDEC911BF31EE700CEB123 /* ItemNode.m */,
8691
);
8792
indentWidth = 2;
8893
path = Sample;
@@ -228,6 +233,7 @@
228233
isa = PBXSourcesBuildPhase;
229234
buildActionMask = 2147483647;
230235
files = (
236+
25FDEC921BF31EE700CEB123 /* ItemNode.m in Sources */,
231237
AC3C4A6A1A11F47200143C57 /* ViewController.m in Sources */,
232238
9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */,
233239
AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* This file provided by Facebook is for non-commercial testing and evaluation
2+
* purposes only. Facebook reserves all rights not expressly granted.
3+
*
4+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
9+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
*/
11+
12+
#import <AsyncDisplayKit/AsyncDisplayKit.h>
13+
14+
@interface ItemNode : ASTextCellNode
15+
16+
- (instancetype)initWithString:(NSString *)string;
17+
18+
@end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* This file provided by Facebook is for non-commercial testing and evaluation
2+
* purposes only. Facebook reserves all rights not expressly granted.
3+
*
4+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
9+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
*/
11+
12+
#import "ItemNode.h"
13+
14+
@implementation ItemNode
15+
16+
- (instancetype)initWithString:(NSString *)string
17+
{
18+
self = [super init];
19+
if (self != nil) {
20+
self.text = string;
21+
[self updateBackgroundColor];
22+
}
23+
return self;
24+
}
25+
26+
- (void)updateBackgroundColor
27+
{
28+
if (self.highlighted) {
29+
self.backgroundColor = [UIColor grayColor];
30+
} else if (self.selected) {
31+
self.backgroundColor = [UIColor darkGrayColor];
32+
} else {
33+
self.backgroundColor = [UIColor lightGrayColor];
34+
}
35+
}
36+
37+
- (void)setSelected:(BOOL)selected
38+
{
39+
[super setSelected:selected];
40+
[self updateBackgroundColor];
41+
}
42+
43+
- (void)setHighlighted:(BOOL)highlighted
44+
{
45+
[super setHighlighted:highlighted];
46+
[self updateBackgroundColor];
47+
}
48+
49+
@end

examples/ASCollectionView/Sample/ViewController.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#import <AsyncDisplayKit/AsyncDisplayKit.h>
1515
#import "SupplementaryNode.h"
16+
#import "ItemNode.h"
1617

1718
@interface ViewController () <ASCollectionViewDataSource, ASCollectionViewDelegateFlowLayout>
1819
{
@@ -78,11 +79,7 @@ - (void)reloadTapped
7879
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath
7980
{
8081
NSString *text = [NSString stringWithFormat:@"[%zd.%zd] says hi", indexPath.section, indexPath.item];
81-
ASTextCellNode *node = [[ASTextCellNode alloc] init];
82-
node.text = text;
83-
node.backgroundColor = [UIColor lightGrayColor];
84-
85-
return node;
82+
return [[ItemNode alloc] initWithString:text];
8683
}
8784

8885
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

0 commit comments

Comments
 (0)