一个TableView的简单Demo 下载:http://download.csdn.net/detail/qqmcy/7219489
HelloWorldScene.h
#include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC_EXT; USING_NS_CC; class HelloWorld : public cocos2d::Layer , public TableViewDataSource, public TableViewDelegate { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx); virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx); virtual ssize_t numberOfCellsInTableView(TableView *table); virtual void tableCellTouched(TableView* table, TableViewCell* cell); void scrollViewDidScroll(cocos2d::extension::ScrollView *view); void scrollViewDidZoom(cocos2d::extension::ScrollView *view); };
#include "HelloWorldScene.h" USING_NS_CC; 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 size = Director::getInstance()->getVisibleSize(); TableView* tableView = TableView::create(this, Size(200, 400)); tableView->setAnchorPoint(Point(0.5f, 0.5f)); tableView->setPosition(Point(size.width / 2, 0)); tableView->setDirection(ScrollView::Direction::VERTICAL); tableView->setDelegate(this); tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN); tableView->reloadData(); this->addChild(tableView, 1); this->setTouchEnabled(true); return true; } Size HelloWorld::tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) { return Size(600, 100); } TableViewCell* HelloWorld::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) { TableViewCell* cell = table->dequeueCell(); if (!cell) { cell = new TableViewCell(); cell->autorelease(); } cell->removeAllChildrenWithCleanup(true); Sprite* testSprite = Sprite::create("CloseNormal.png"); testSprite->setAnchorPoint(Point(0.5f, 0.5f)); testSprite->setPosition(Point(200, idx * 50 + 100)); Size size = testSprite->getContentSize(); testSprite->setContentSize(Size(100, 100)); cell->addChild(testSprite); return cell; } ssize_t HelloWorld::numberOfCellsInTableView(cocos2d::extension::TableView *table) { return 40; } void HelloWorld::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell) { } void HelloWorld::scrollViewDidScroll(cocos2d::extension::ScrollView *view) { } void HelloWorld::scrollViewDidZoom(cocos2d::extension::ScrollView *view) { } void HelloWorld::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }