您的位置:首页 > 移动开发 > Cocos引擎

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

2014-03-13 15:55 561 查看
提示:请在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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: