【Cocos2d-X】TableView的使用,cocos2d-xtableview

來源:互聯網
上載者:User

【Cocos2d-X】TableView的使用,cocos2d-xtableview

在Cocos2d-x使用TableView的過程如下:

  • 首先用一個類繼承CCTableViewDelegate(代理)和CCTableViewDataSource(資料來源);
  • 然後實現裡面的有關tableView操作和內容的四個抽象方法;
  • 最後就可以在情境類中通過CCTableView來使用這個類,CCTableView會分別設定代理對象和資料來源對象

樣本:
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:    //================    //需實現的方法    //================    //處理觸摸事件    virtual void tableCellTouched(cocos2d::extension::CCTableView* table,     cocos2d::extension::CCTableViewCell* cell);    //每一項的寬度和高度    virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);    //產生列表每一項的內容unsigned int idx);    //一共多少項    virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);    //================    //可忽略的方法    //================    //繼承自scrollView所需要的方法,可以空函數體忽略    virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view){}    virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view){}};#endif

TableView.cpp

#include "TableView.h"//每個儲存格選項的大小CCSize TableView::cellSizeForTable(extension::CCTableView *table){    return CCSizeMake(480, 320);}//單個選項的顯示extension::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;}//選項個數unsigned int TableView::numberOfCellsInTableView(extension::CCTableView *table){    return 5;}//點擊儲存格選項的響應事件void TableView::tableCellTouched(extension::CCTableView* table, extension::CCTableViewCell* cell){    char s[20];    sprintf(s, "the cell is %d", cell->getIdx());    CCMessageBox(s, "對話方塊");}

在HelloWorld::init方法裡的使用TableView的部分代碼:

//建立一個自己的TableViewTableView* tv = new TableView();                //此處的tv綁定的是資料來源cocos2d::extension::CCTableView *tableView = cocos2d::extension::CCTableView::create(tv, CCSizeMake(480, 320));              //橫向排列  tableView->setDirection(cocos2d::extension::kCCScrollViewDirectionHorizontal);  //放在螢幕中央tableView->setPosition(ccp(visibleSize.width / 2 - 240, visibleSize.height / 2 - 160)); //此處的tv是代理tableView->setDelegate(tv);tableView->setVerticalFillOrder(cocos2d::extension::kCCTableViewFillTopDown);this->addChild(tableView);tableView->reloadData();


點擊某一圖片後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.