cocos2d-x CCTableView、CCScrollView的使用、自定义CCTableViewCell
2014-03-13 15:55
561 查看
提示:请在appdelegate下改设计分辨率为(1136X768)如下
pEGLView->setDesignResolutionSize(1136,
768, kResolutionShowAll);
一、头文件(MyTable.h)
二、MyTable.cpp源文件
三、使用方法
// 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
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/26/a8eb4a07b57e01898efbf1e54f3c4743)
四、改成横向方法
1、在MyTable.cpp顶部加入:
#define SZ_TABLE2 CCSizeMake(1136,
100)
2、在MyTable::init()方法中把前两行修改为
m_tableView = CCTableView::create(this,SZ_TABLE2);
m_tableView->setDirection(kCCScrollViewDirectionHorizontal);
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);
相关文章推荐
- cocos2d-x CCScrollView和CCTableView的使用
- cocos2d-x 源码 :可以循环的CCScrollView (代码已经重构过,附使用方法)
- cocos2d-x CCScrollView和CCTableView的使用
- cocos2d-x中的CCScrollView的使用
- cocos2d-x CCScrollView和CCTableView的使用
- cocos2d-x学习日志(7) --CCScrollView和CCTableView的使用
- cocos2d-x CCScrollView和CCTableView的使用
- Cocos2d-x中的CCScrollView的使用
- cocos2d-x CCScrollView和CCTableView的使用(转载)
- cocos2d-x学习日志(7) --CCScrollView和CCTableView的使用
- cocos2d-x CCScrollView和CCTableView的使用
- Cocos2d中CCScrollView和CCTableView使用
- cocos2d-x CCScrollView和CCTableView的使用
- Cocos2d-x学习(十):cocos2d-x 2.0版本中CCScrollView的用法
- cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件
- Cocos2d-x CCScrollView demo
- CCScrollView 使用小记
- CCScrollView的使用
- cocos2d-x 2.0版本中CCScrollView的用法
- cocos2d-x tableview和CCControlButton的使用