Skip to content

Commit 23bfa0d

Browse files
Support custom CCTableViewCell
1 parent 7068735 commit 23bfa0d

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

cocos2d-ui/CCTableView.h

100644100755
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@
2727
@class CCButton;
2828
@class CCTableView;
2929

30+
31+
#pragma mark CCTableViewCellProtocol
32+
33+
/** Requirements for implementing a custom Table View Cell.
34+
Note that the custom cell MUST inherit CCNode.*/
35+
36+
@protocol CCTableViewCellProtocol <NSObject>
37+
38+
@property (nonatomic, assign) NSUInteger index;
39+
- (void) pressedCell:(id)sender;
40+
41+
@end
42+
3043
#pragma mark CCTableViewCell
3144

3245
/** Represents a cell in a CCTableView. It is essentially a thin wrapper around CCButton that allows the user to interact with the cell.
3346
You can add any node(s) as content to the cell. */
34-
@interface CCTableViewCell : CCNode
47+
@interface CCTableViewCell : CCNode <CCTableViewCellProtocol>
3548
{
36-
NSUInteger _index;
49+
3750
}
3851

3952
/** The CCButton instance used to allow interaction with the cell. */
@@ -56,7 +69,7 @@
5669
@param tableView The CCTableView that is requesting a cell for the index.
5770
@param index The index of the cell that is requested.
5871
@returns The CCTableViewCell for the given index. */
59-
- (CCTableViewCell*) tableView:(CCTableView*)tableView nodeForRowAtIndex:(NSUInteger) index;
72+
- (id<CCTableViewCellProtocol>) tableView:(CCTableView*)tableView nodeForRowAtIndex:(NSUInteger) index;
6073
/** Requests the number of rows in the given table view.
6174
@param tableView The CCTableView for which the number of rows should be returned.
6275
@returns The number of rows in the table view. */
@@ -211,5 +224,6 @@
211224
@param target The object that should receive the selector.
212225
@param selector The selector to run, ie `@selector(onRowSelected:)`. */
213226
-(void) setTarget:(id)target selector:(SEL)selector;
227+
-(void) selectedRow:(NSUInteger)row;
214228

215229
@end

cocos2d-ui/CCTableView.m

100644100755
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ - (void) selectedRow:(NSUInteger) row;
3838

3939
@interface CCTableViewCellHolder : NSObject
4040

41-
@property (nonatomic,strong) CCTableViewCell* cell;
41+
@property (nonatomic,strong) id<CCTableViewCellProtocol> cell;
4242

4343
@end
4444

@@ -67,12 +67,13 @@ - (void) setPosition:(CGPoint)position
6767

6868
@interface CCTableViewCell (Helper)
6969

70-
@property (nonatomic,assign) NSUInteger index;
71-
7270
@end
7371

72+
7473
@implementation CCTableViewCell
7574

75+
@synthesize index;
76+
7677
- (id) init
7778
{
7879
self = [super init];
@@ -94,16 +95,6 @@ - (void) pressedCell:(id)sender
9495
[(CCTableView*)(self.parent.parent) selectedRow:self.index];
9596
}
9697

97-
- (void) setIndex:(NSUInteger)index
98-
{
99-
_index = index;
100-
}
101-
102-
- (NSUInteger) index
103-
{
104-
return _index;
105-
}
106-
10798
@end
10899

109100

@@ -244,7 +235,7 @@ - (void) showRowsForRange:(NSRange)range
244235
CCTableViewCellHolder* holder = [_rows objectAtIndex:oldIdx];
245236
if (holder)
246237
{
247-
[self.contentNode removeChild:holder.cell cleanup:YES];
238+
[self.contentNode removeChild:(CCNode*)holder.cell cleanup:YES];
248239
holder.cell = NULL;
249240
}
250241
}
@@ -255,18 +246,23 @@ - (void) showRowsForRange:(NSRange)range
255246
if (!NSLocationInRange(newIdx, _currentlyVisibleRange))
256247
{
257248
CCTableViewCellHolder* holder = [_rows objectAtIndex:newIdx];
249+
CCNode* node;
258250
if (!holder.cell)
259251
{
260252
holder.cell = [_dataSource tableView:self nodeForRowAtIndex:newIdx];
261253
holder.cell.index = newIdx;
262-
holder.cell.position = CGPointMake(0, [self locationForCellWithIndex:newIdx]);
263-
holder.cell.positionType = CCPositionTypeMake(CCPositionUnitPoints, CCPositionUnitPoints, CCPositionReferenceCornerTopLeft);
264-
holder.cell.anchorPoint = CGPointMake(0, 1);
254+
255+
node = (CCNode*)holder.cell;
256+
node.position = CGPointMake(0, [self locationForCellWithIndex:newIdx]);
257+
node.positionType = CCPositionTypeMake(CCPositionUnitPoints, CCPositionUnitPoints, CCPositionReferenceCornerTopLeft);
258+
node.anchorPoint = CGPointMake(0, 1);
259+
} else {
260+
node = (CCNode*)holder.cell;
265261
}
266262

267263
if (holder.cell)
268264
{
269-
[self.contentNode addChild:holder.cell];
265+
[self.contentNode addChild:node];
270266
}
271267
}
272268
}
@@ -395,7 +391,7 @@ - (void) setTarget:(id)target selector:(SEL)selector
395391
}];
396392
}
397393

398-
- (void) selectedRow:(NSUInteger) row
394+
- (void) selectedRow:(NSUInteger)row
399395
{
400396
self.selectedRow = row;
401397
[self triggerAction];

0 commit comments

Comments
 (0)