cocos2d-x3.0RC2 TableViewDemo

來源:互聯網
上載者:User

一個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);    };

HelloWorldScene.cpp

#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 instancebool 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}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.