本文主要是介绍cocos2d-x中CCTableView的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/azhou_hui/article/details/8167498
CCTableView用来实现滑动列表的效果。本来想用CCScrollView,不过好像有bug,一直达不到效果。最后还是选用了CCTableView
------------------------------------------------------------------------------------相关代码----------------------------------------------------------------------------
(1)CustomTableViewCell.h
- #ifndef __CUSTOMTABELVIEWCELL_H__
- #define __CUSTOMTABELVIEWCELL_H__
- #include "cocos2d.h"
- #include "cocos-ext.h"
- class CustomTableViewCell : public cocos2d::extension::CCTableViewCell
- {
- public:
- virtual void draw();
- };
- #endif /* __CUSTOMTABELVIEWCELL_H__ */
(2)CustomTableViewCell.cpp
- #include "CustomTableViewCell.h"
- USING_NS_CC;
- void CustomTableViewCell::draw()
- {
- CCTableViewCell::draw();
- // draw bounding box
- // CCPoint pos = getPosition();
- // CCSize size = CCSizeMake(178, 200);
- // CCPoint vertices[4]={
- // ccp(pos.x+1, pos.y+1),
- // ccp(pos.x+size.width-1, pos.y+1),
- // ccp(pos.x+size.width-1, pos.y+size.height-1),
- // ccp(pos.x+1, pos.y+size.height-1),
- // };
- // ccDrawColor4B(0, 0, 255, 255);
- // ccDrawPoly(vertices, 4, true);
- }
(3)TableViewMenuLayer.h
- #ifndef __TABLEVIEWMENULAYER_H__
- #define __TABLEVIEWMENULAYER_H__
- //城市界面右边的滑动菜单项列表Layer,这个用的是CCTableView
- #include "cocos2d.h"
- #include "cocos-ext.h"
- using namespace cocos2d;
- using namespace cocos2d::extension;
- class TableViewMenuLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate
- {
- public:
- static TableViewMenuLayer *instance;
- public:
- TableViewMenuLayer(void);
- ~TableViewMenuLayer(void);
- public:
- virtual bool init();
- CREATE_FUNC(TableViewMenuLayer);
- virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {};
- virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}
- virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
- virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
- virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
- virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
- };
- #endif //__TABLEVIEWMENULAYER_H__
(4)TableViewMenuLayer.cpp
- #include "TableViewMenuLayer.h"
- #include "CustomTableViewCell.h"
- #include "MenuInfoLayer.h"
- using namespace cocos2d;
- using namespace cocos2d::extension;
- TableViewMenuLayer::TableViewMenuLayer(void)
- {
- }
- TableViewMenuLayer::~TableViewMenuLayer(void)
- {
- }
- //初始化静态成员
- TableViewMenuLayer* TableViewMenuLayer::instance=NULL;
- // on "init" you need to initialize your instance
- bool TableViewMenuLayer::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- // Add the BG
- CCSprite* tableViewBG = CCSprite::create("right.png");
- tableViewBG->setPosition(ccp(winSize.width-45, winSize.height/2));
- this->addChild(tableViewBG);
- CCSprite* upBG = CCSprite::create("up.png");
- upBG->setPosition(ccp(winSize.width-45, winSize.height-30));
- this->addChild(upBG);
- CCSprite* downBG = CCSprite::create("down.png");
- downBG->setPosition(ccp(winSize.width-45, 10));
- this->addChild(downBG);
- // Add the CCTableView
- CCTableView *tableView = CCTableView::create(this, CCSizeMake(80, 690));
- tableView->setDirection(kCCScrollViewDirectionVertical);
- tableView->setPosition(ccp(winSize.width-85,28));
- tableView->setDelegate(this);
- tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
- this->addChild(tableView);
- tableView->reloadData();
- return true;
- }
- void TableViewMenuLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
- {
- //打印相应cell的Idx
- CCLOG("cell touched at index: %i", cell->getIdx());
- //调用相应的菜单功能
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- // add the MenuInfoLayer
- if (this->getChildByTag(101))
- {
- this->removeChildByTag(101,true);
- }
- MenuInfoLayer *menuInfoLayer = MenuInfoLayer::create();
- this->addChild(menuInfoLayer,0,101);
- menuInfoLayer->runAction(CCMoveBy::create(0.3,ccp(-size.width,0)));
- }
- CCSize TableViewMenuLayer::cellSizeForTable(CCTableView *table)
- {
- return CCSizeMake(80, 115);
- }
- CCTableViewCell* TableViewMenuLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
- {
- CCString *string;
- switch(idx)
- {
- case 0: string = CCString::createWithFormat("%s", "CITY"); break;
- case 1: string = CCString::createWithFormat("%s", "WORLD"); break;
- case 2: string = CCString::createWithFormat("%s", "BATTLE"); break;
- case 3: string = CCString::createWithFormat("%s", "CityInfo"); break;
- case 4: string = CCString::createWithFormat("%s", "Commander"); break;
- case 5: string = CCString::createWithFormat("%s", "Equipment"); break;
- case 6: string = CCString::createWithFormat("%s", "Quest"); break;
- case 7: string = CCString::createWithFormat("%s", "Items"); break;
- case 8: string = CCString::createWithFormat("%s", "Military"); break;
- case 9: string = CCString::createWithFormat("%s", "Legion"); break;
- case 10: string = CCString::createWithFormat("%s", "Rank"); break;
- case 11: string = CCString::createWithFormat("%s", "Report"); break;
- case 12: string = CCString::createWithFormat("%s", "News"); break;
- case 13: string = CCString::createWithFormat("%s", "System"); break;
- case 14: string = CCString::createWithFormat("%s", "Shop"); break;
- case 15: string = CCString::createWithFormat("%s", "Friend"); break;
- default:string = CCString::createWithFormat("%s", "Error");
- }
- // the cell
- CCTableViewCell *cell = table->dequeueCell();
- if (!cell) {
- // the sprite
- cell = new CustomTableViewCell();
- cell->autorelease();
- CCSprite *sprite = CCSprite::create("CloseNormal.png");
- sprite->setAnchorPoint(ccp(0, 0));
- sprite->setPosition(ccp(0, 30));
- sprite->setTag(456);
- cell->addChild(sprite);
- // the label
- CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Thonburi", 13);
- label->setPosition(ccp(40, 20));
- label->setTag(123);
- cell->addChild(label);
- }
- else
- {
- // the sprite
- cell->removeChildByTag(456,true);
- CCSprite *sprite ;
- switch(idx)
- {
- case 0: sprite = CCSprite::create("city.png");break;
- case 1: sprite = CCSprite::create("world.png");break;
- case 2: sprite = CCSprite::create("battle.png");break;
- case 3: sprite = CCSprite::create("cityInfo.png");break;
- case 4: sprite = CCSprite::create("hero.png");break;
- case 5: sprite = CCSprite::create("equipment.png");break;
- case 6: sprite = CCSprite::create("task.png");break;
- case 7: sprite = CCSprite::create("treasure.png");break;
- case 8: sprite = CCSprite::create("army.png");break;
- case 9: sprite = CCSprite::create("guild.png");break;
- case 10: sprite = CCSprite::create("rank.png");break;
- case 11: sprite = CCSprite::create("report.png");break;
- case 12: sprite = CCSprite::create("message.png");break;
- case 13: sprite = CCSprite::create("system.png");break;
- case 14: sprite = CCSprite::create("mall.png");break;
- case 15: sprite = CCSprite::create("friend.png");break;
- default:sprite = CCSprite::create("CloseNormal.png");
- }
- sprite->setAnchorPoint(ccp(0, 0));
- sprite->setPosition(ccp(0, 35));
- sprite->setTag(456);
- cell->addChild(sprite);
- // the label
- CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);
- label->setString(string->getCString());
- }
- return cell;
- }
- unsigned int TableViewMenuLayer::numberOfCellsInTableView(CCTableView *table)
- {
- return 16;
- }
- (5)CityScene.cpp 调用界面,调用时直接在scene里调用该layer(包含包含的头文件)
- <pre class="cpp" name="code">#include "TableViewMenuLayer.h"
- // add TableViewMenuLayer
- TableViewMenuLayer *tableViewMenuLayer = TableViewMenuLayer::create();
- TableViewMenuLayer::instance=tableViewMenuLayer;
- this->addChild(tableViewMenuLayer,10);</pre>
- <pre></pre>
- <pre></pre>
这篇关于cocos2d-x中CCTableView的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!