首页 > 其他 > 详细

cocos2d-x CCTableView、CCScrollView的使用、自定义CCTableViewCell

时间:2014-03-14 01:36:04      阅读:745      评论:0      收藏:0      [点我收藏+]

一、头文件(MyTable.h)

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

//单元格
class Cell  : public cocos2d::extension::CCTableViewCell
{
public:
    static Cell *create(int idx);
    void init(int idx);
    //改变颜色.isNewColor是否为新的颜色
    void changeColor(bool isNewColor);
    //重置
    void reset(int idx, int m_selected);
    
private:
    cocos2d::CCLabelTTF *m_lbl;                 //标签
    cocos2d::CCSprite *m_sprite;                //背景图片
    cocos2d::ccColor3B m_oriColor, m_newColor;  //原来颜色、新颜色
};



class MyTable : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDelegate, public cocos2d::extension::CCTableViewDataSource
{
public:
    virtual bool init();
    CREATE_FUNC(MyTable);

    //表格代理方法
    virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
    virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
    virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
    virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
    virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);
    virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view);
    
    
private:
    int m_selected;                         //选中的cellId
    cocos2d::extension::CCTableView *m_tableView;               //tableView
};


二、MyTable.cpp源文件

#include "MyTable.h"
using namespace cocos2d;
using namespace cocos2d::extension;

#define SZ_TABLE CCSizeMake(355, 568)
#define SZ_CELL CCSizeMake(350, 100)

Cell *Cell::create(int idx)
{
    Cell *cell = new Cell();
    cell->init(idx);
    cell->autorelease();
    return cell;
}

void Cell::init(int idx)
{
    m_oriColor = ccc3(0, 0, 0);
    m_newColor = ccc3(255, 0, 0);
    
    //添加背景单元格
    m_sprite = CCSprite::create("button.png");
    m_sprite->setPosition(ccp(SZ_TABLE.width / 2, SZ_CELL.height / 2.0));
    this->addChild(m_sprite);
    
    
    //添加用户名标签
    CCString *str = CCString::createWithFormat("第 %d 个", idx + 1);
    m_lbl=CCLabelTTF::create(str->getCString(), "Arial", 45.0);
    m_lbl->setColor(m_oriColor);
    m_lbl->setPosition(ccp(SZ_CELL.width / 2, SZ_CELL.height / 2.0));
    this->addChild(m_lbl);
  }

//改变颜色.isNewColor是否为新的颜色
void Cell::changeColor(bool isNewColor)
{
    isNewColor ? m_lbl->setColor(m_newColor) : m_lbl->setColor(m_oriColor);
}

//重置
void Cell::reset(int idx, int m_selected)
{
    CCString *str = CCString::createWithFormat("第 %d 个", idx + 1);
    m_lbl->setString(str->getCString());
    changeColor(m_selected == idx);
    m_sprite->setTexture(CCTextureCache::sharedTextureCache()->addImage("button.png"));
}




bool MyTable::init()
{
    if(!CCLayer::init())
    {return false;}
    
    m_selected = 0;
    
    //添加tableView
    m_tableView = CCTableView::create(this, SZ_TABLE);
    m_tableView->setDirection(kCCScrollViewDirectionVertical);
    m_tableView->setAnchorPoint(CCPointZero);
    m_tableView->setPosition(CCPointZero);
    m_tableView->setDelegate(this);
    m_tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
    this->addChild(m_tableView);

    return true;
}

//表格代理方法
void MyTable::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
    if(m_selected == cell->getIdx())
    {
        return;
    }
    
    //修改原来的单元格
    Cell *c = dynamic_cast<Cell *>(m_tableView->cellAtIndex(m_selected));
    c ? c->changeColor(false) : (void)NULL;
    
    
    //修改新的单元格颜色
    c = dynamic_cast<Cell *>(cell);
    c->changeColor(true);
    
    m_selected = cell->getIdx();

}

CCTableViewCell* MyTable::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    Cell *cell = dynamic_cast<Cell *>(table->dequeueCell());
    
    if(!cell)
    {
        cell = Cell::create(idx);
        cell->changeColor(m_selected == idx);
        return cell;
    }
   
    cell->reset(idx, m_selected);
    return cell;
}

unsigned int MyTable::numberOfCellsInTableView(CCTableView *table)
{
    return 50;
}

CCSize MyTable::cellSizeForTable(CCTableView *table)
{
    return SZ_CELL;
}
void MyTable::scrollViewDidScroll(CCScrollView* view)
{
    
}
void MyTable::scrollViewDidZoom(CCScrollView* view)
{
    printf("scrollViewDidZoom");
}


三、使用方法

// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    

    this->addChild(MyTable::create());


    return true;

}


button.png

bubuko.com,布布扣


cocos2d-x CCTableView、CCScrollView的使用、自定义CCTableViewCell,布布扣,bubuko.com

cocos2d-x CCTableView、CCScrollView的使用、自定义CCTableViewCell

原文:http://blog.csdn.net/zwc2xm/article/details/21174355

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!