cocos2d-x之TableView列表

cocos2d-x之TableView列表

HelloWorld.h


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include <cocos-ext.h>

USING_NS_CC_EXT;
USING_NS_CC;//至關於using namespace cocos2d;

//使類繼承TableViewDataSource類型添加列表項,繼承TabelViewDelegate添加事件監聽器
class HelloWorld : public cocos2d::Layer,TableViewDataSource,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();

    // 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
     */
    //特定的位置的table的列表項的大小
    //設置大小,Size 的命名空間爲cocos2d
    virtual Size cellSizeForTable(TableView *table);
    /**
     * 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);
    /**
     * Returns number of cells in a given table view.
     *
     * @return number of cells
     */
    
    //列表裏面一共有多少個列表項,返回100,就是有一百個
    virtual ssize_t numberOfCellsInTableView(TableView *table);
    
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);
    /**
     * @js NA
     * @lua NA
     */
    virtual void scrollViewDidScroll(ScrollView* view) {};
    /**
     * @js NA
     * @lua NA
     */
    virtual void scrollViewDidZoom(ScrollView* view) {};
    
};

#endif // __HELLOWORLD_SCENE_H__



HelloWorld.cpp

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

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;
    }
    //建立列表項
    TableView *tv = TableView::create(this, Size(300, 300));
    
    tv->setAnchorPoint(Point(0, 0));//設置錨點
    
    tv->setPosition(100, 0);//設置位置
    
    tv->setDelegate(this);//設置列表項的事件監聽器
    
    addChild(tv);//將列表項添加層中
    
    return true;
}

Size HelloWorld::cellSizeForTable(cocos2d::extension::TableView *table){
    return Size(300, 50);
}


TableViewCell* HelloWorld::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx){
    
    /**獲取到一個table cell,若沒有則返回值爲空,若可以獲取到cell,則返回值不爲空
     若是一個列表項曾經呈現過,可是在拖動列表項時,被隱藏了,被拖出界面時,
     這個列表項會被回收,會被放在一個table cell的一個隊列中,
     若是還有新的列表項須要呈現時,就先在這個回收的隊列中查找,
     看看有沒有被回收的,若是有回收的則直接使用,沒有的話,從新建立
     */
    TableViewCell *cell = table->dequeueCell();
    
    LabelTTF *label;//定義一個文本標籤,在下面建立
    
    //若是回收隊列中沒有回收的,則建立列表項
    if(cell == NULL){
        cell = TableViewCell::create();//建立新的列表項
        label = LabelTTF::create();//建立文本標籤
        label->setTag(2);//建立label的標籤,標籤爲2
        label->setFontSize(30);//設置label的文字大小
        label->setAnchorPoint(Point(0, 0));//設置label的錨點
        cell->addChild(label);//將文本添加到列表項中
        
    }else{//若是可以獲取到被回收的列表項時,就從回收的列表項中獲取子對象就是label子對象
        label = (LabelTTF*)cell->getChildByTag(2);//獲取標籤
    }
    
    //用cocos2d中包裝的一個字符工具
    label->setString(StringUtils::format("label %ld",idx));
    
    return cell;//返回cell
}

ssize_t HelloWorld::numberOfCellsInTableView(cocos2d::extension::TableView *table){
    return 100;//定義一個100個的列表項
}

//獲取被點擊的列表項cell,能夠獲取到被點擊的列表項的內部數據
void HelloWorld::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell){
    LabelTTF *label = (LabelTTF*)cell->getChildByTag(2);
    log("%s",label->getString().c_str());//獲取點擊的列表項的內容
}

iphone

相關文章
相關標籤/搜索