Cocos2d-X: Use of TableView, cocos2d-xtableview
The process of using TableView in a Cocos2d-x is as follows:
- First, use a class to inherit the CCTableViewDelegate (proxy) and CCTableViewDataSource (data source );
- Then implement the four Abstract methods for tableView operations and content;
- Finally, you can use CCTableView in the scenario class to use this class. CCTableView sets the proxy object and data source object respectively.
Example:
TableView. h
# Ifndef _ TABELVIEW_H _ # define _ TABLEVIEW_H _ # include "cocos2d. h "# include" cocos-ext.h "using namespace cocos2d; class TableView: public cocos2d: extension: CCTableViewDelegate, public cocos2d: extension: CCTableViewDataSource {public: // ============================/// method to be implemented /// ====================== ==// handle the touch event virtual void tableCellTouched (cocos2d:: extension: CCTableView * table, cocos2d: extension: CCTableViewCell * cell); // the width and height of each item. virtual cocos2d: CCSize cellSizeForTable (cocos2d: extension :: CCTableView * table); // generate the content of each item in the list unsigned int idx); // Total number of items virtual unsigned int numberOfCellsInTableView (cocos2d: extension: CCTableView * table ); // ==========================/// methods that can be ignored ==// inherit the method required by scrollView, empty function body can ignore virtual void scrollViewDidScroll (cocos2d: extension: CCScrollView * view) {} virtual void scrollViewDidZoom (cocos2d: extension: CCScrollView * view ){}}; # endif
TableView. cpp
# Include "TableView. h "// the size of each cell option CCSize TableView: cellSizeForTable (extension: CCTableView * table) {return CCSizeMake (480,320);} // display extension for a single option :: CCTableViewCell * TableView: tableCellAtIndex (extension: CCTableView * table, unsigned int idx) {extension: CCTableViewCell * cell = table-> dequeueCell (); if (! Cell) {// the sprite cell = new extension: CCTableViewCell (); cell-> autorelease (); CCSprite * sprite = CCSprite: create ("HelloWorld.png "); sprite-> setAnchorPoint (ccp (0, 0); sprite-> setPosition (ccp (0, 30); cell-> addChild (sprite);} return cell ;} // number of options unsigned int TableView: numberOfCellsInTableView (extension: CCTableView * table) {return 5;} // click the response event void TableView: tableCellTouched (extension :: CCTableView * table, extension: CCTableViewCell * cell) {char s [20]; sprintf (s, "the cell is % d", cell-> getIdx ()); CCMessageBox (s, "dialog box ");}
Some code for using TableView in the HelloWorld: init method:
// Create your own TableViewTableView * TV = new TableView (); // The TV is bound to the data source cocos2d: extension: CCTableView * tableView = cocos2d: extension :: CCTableView: create (TV, CCSizeMake (480,320); // horizontally arrange tableView-> setDirection (cocos2d: extension: kCCScrollViewDirectionHorizontal ); // In the center of the screen tableView-> setPosition (ccp (visibleSize. width/2-240, visibleSize. height/2-160); // the TV here is the proxy tableView-> setDelegate (TV); tableView-> setVerticalFillOrder (cocos2d: extension: kCCTableViewFillTopDown ); this-> addChild (tableView); tableView-> reloadData ();
:
After clicking an image: