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());//獲取點擊的列表項的內容
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM