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

提示:请在appdelegate下改设计分辨率为(1136X768)如下

    pEGLView->setDesignResolutionSize(1136, 768, kResolutionShowAll);


一、头文件(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




四、改成横向方法

1、在MyTable.cpp顶部加入:

#define SZ_TABLE2 CCSizeMake(1136, 100)


2、在MyTable::init()方法中把前两行修改为

 m_tableView = CCTableView::create(this,SZ_TABLE2);

 m_tableView->setDirection(kCCScrollViewDirectionHorizontal);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值