TableView就相当于Android中的ListView吧!(个人理解),就是显示一个列表相的view
cpp代码
#include "HelloWorldScene.h"
USING_NS_CC;
USING_NS_CC_EXT;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
//TableView
auto tableView = TableView::create(this, Size(300, 500));
tableView->setAnchorPoint(Point(0, 0));
tableView->setPosition(0, 0);
//注册监听事件
tableView->setDelegate(this);
this->addChild(tableView);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
h文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class HelloWorld : public cocos2d::Layer, public TableViewDataSource,public TableViewDelegate
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
public :
/**
* cell height for a given table.
*
* @param table table to hold the instances of Class
* @return cell size
*/
virtual Size cellSizeForTable(TableView *table) {
return Size(300,50);
};
/**
* a cell instance at a given index
*
* @param idx index to search for a cell
* @return cell found at idx
*/
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx)
{
TableViewCell* cell = table->dequeueCell();
LabelTTF * label;
if (cell == NULL)
{
cell = TableViewCell::create();
label = LabelTTF::create();
label->setTag(2);
label->setFontSize(30);
label->setAnchorPoint(Point(0, 0));
label->setPosition(0, 0);
cell->addChild(label);
}
else
{
label = (LabelTTF*)cell->getChildByTag(2);
}
label->setString(StringUtils::format("Label %ld", idx));
return cell;
}
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual ssize_t numberOfCellsInTableView(TableView *table)
{
return 100;
}
public :
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
* @js NA
* @lua NA
*/
virtual void tableCellTouched(TableView* table, TableViewCell* cell)
{
LabelTTF * label = (LabelTTF*)cell->getChildByTag(2);
log("Lable-->%s", label->getString().c_str());
}
/**
* Delegate to respond a table cell press event.
*
* @param table table contains the given cell
* @param cell cell that is pressed
* @js NA
* @lua NA
*/
virtual void tableCellHighlight(TableView* table, TableViewCell* cell){};
/**
* Delegate to respond a table cell release event
*
* @param table table contains the given cell
* @param cell cell that is pressed
* @js NA
* @lua NA
*/
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell){};
/**
* Delegate called when the cell is about to be recycled. Immediately
* after this call the cell will be removed from the scene graph and
* recycled.
*
* @param table table contains the given cell
* @param cell cell that is pressed
* @js NA
* @lua NA
*/
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell){};
};
#endif // __HELLOWORLD_SCENE_H__
代码不够规范,仅供交流学习使用!