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());//获取点击的列表项的内容
}

posted @ 2015-03-28 08:13  silent-bobo  阅读(2712)  评论(0编辑  收藏  举报