CCTableView Class Reference

Inherits from CCScrollView : CCNode : CCResponder : NSObject
Declared in CCTableView.h

Overview

A vertical table view that resembles (but is technically incompatible to) UITableView.

A table view consists of one or more rows, where each row is a CCTableViewCell node. The height of each row is the same but the content’s height can vary depending on the CCTableViewCell contents. It is entirely up to you to adhere to the row height of the table view, or not to (if it makes sense). For instance you could have parts of a preceeding row blend into the next row.

You must assign a dataSource, which is any object implementing the CCTableViewDataSource protocol. The data source returns CCTableViewCell instances when requested. The cells make up the table view’s content.

A minimal table view with touch input looks like this:

Objective-C:

CCTableView* tableView = [CCTableView node];
tableView.dataSource = self;
tableView.block = ^(CCTableView* tableView) {
    NSLog(@"Selected cell at index: %i", (int)tableView.selectedRow);
};
[self addChild:tableView];

Swift:

let tableView = CCTableView()
tableView.dataSource = self
tableView.block = { (tableView) in
    NSLog("Selected cell at index: %i", Int(tableView.selectedRow))
}
addChild(tableView)

Of course self here needs to implement the CCTableViewDataSource protocol. A simple example implementation of the proctocol with 8 rows and all rows the same height looks like this:

Objective-C:

-(CCTableViewCell*) tableView:(CCTableView*)tableView nodeForRowAtIndex:(NSUInteger)index {
    // use the same seed so that the random colors don't change when scrolling
    srandom(index);

    CCSprite* icon = [CCSprite spriteWithImageNamed:@"Settings.png"];
    icon.color = [CCColor colorWithRed:CCRANDOM_0_1() green:CCRANDOM_0_1() blue:CCRANDOM_0_1()];
    icon.anchorPoint = CGPointZero;

    CCTableViewCell* cell = [CCTableViewCell node];
    cell.contentSize = icon.contentSize;
    [cell addChild:icon];

    _rowHeight = cell.contentSize.height;
    return cell;
}

-(NSUInteger) tableViewNumberOfRows:(CCTableView*) tableView {
    return 8;
}

-(float) tableView:(CCTableView*)tableView heightForRowAtIndex:(NSUInteger)index {
    return _rowHeight;
}

Swift:

The _rowHeight is an ivar of type CGFloat.

Refer to the CCTableViewTest for a code sample.

Warning: Do not add nodes directly to the CCTableView. You must create rows via the CCTableViewDataSource.

Working with the Data Source

  dataSource

An object implementing the CCTableViewDataSource protocol. The data source provides the table with cells that make up the table view’s content.

@property (nonatomic, strong) id<CCTableViewDataSource> dataSource

Discussion

Note: Assigning a new or different data source immediately calls reloadData.

Declared In

CCTableView.h

– reloadData

Removes all cells from memory and requests a new set of cells from the dataSource. Assigning a different dataSource and changing the rowHeight will cause reloadData to run.

- (void)reloadData

Discussion

Warning: Depending on which and how many nodes are in the table view and how the dataSource is implemented this operation can be potentially expensive as cells are first removed from the table view and requested anew from the dataSource.

Declared In

CCTableView.h

Working with Rows

  rowHeight

The height of the rows. The unit depends on rowHeightUnit and defaults to points.

@property (nonatomic, assign) CGFloat rowHeight

Discussion

Note: Changing the row height calls reloadData.

Declared In

CCTableView.h

  rowHeightUnit

The size scale type for row Height, one of CCSizeUnit. Defaults to CCSizeUnitPoints (rowHeight is in points).

@property (nonatomic, assign) CCSizeUnit rowHeightUnit

Declared In

CCTableView.h

  rowHeightInPoints

Returns the rowHeight in points, properly converting the rowHeight value based on rowHeightUnit.

@property (nonatomic, readonly) CGFloat rowHeightInPoints

Declared In

CCTableView.h

  selectedRow

The index of the currently selected row.

@property (nonatomic, assign) NSUInteger selectedRow

Declared In

CCTableView.h

Running Code when a Cell gets selected

  block

Block that is executed when a row is selected (tapped, clicked).

@property (nonatomic, copy) void ( ^ ) ( id sender ) block

Discussion

Objective-C:

tableView.block = ^(id sender) {
    NSLog(@"row selected: %i", (int)tableView.selectedRow);
};

Swift:

tableView.block = {(sender: AnyObject!) in
    NSLog("row selected: %i", Int(tableView.selectedRow))
}

Declared In

CCTableView.h

– setTarget:selector:

Selector that is executed when a row is selected (tapped, clicked). The selector must take one parameter of type id and return void:

- (void)setTarget:(id)target selector:(SEL)selector

Parameters

target

The object that should receive the selector.

selector

The selector to run, ie @selector(onRowSelected:).

Discussion

Objective-C:

-(void) onRowSelected:(id)sender {
}

Swift:

func onRowSelected(sender: AnyObject!) {
}

Declared In

CCTableView.h