转自:http://blog.csdn.net/kuloveyouwei/article/details/9099361
在这篇文章中,我们将讲下CCTableView,顾名思义,CCTableView表试图,在ios里是tableview,android里是listview,都是差不多的意思,在cocos2dx中CCTableView继承自CCScrollview。OK,下面我们直接上代码:
在头文件.h中:
- class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,cocos2d::extension::CCTableViewDataSource
- {
- public:
-
- virtual bool init();
-
-
- static cocos2d::CCScene* scene();
-
-
- void menuCloseCallback(CCObject* pSender);
-
-
- CREATE_FUNC(HelloWorld);
-
-
-
- public:
-
-
- 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);
-
-
- virtual void tableCellHighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
-
-
- virtual void tableCellUnhighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
-
-
-
- void scrollBar(cocos2d::extension::CCTableView* table);
-
-
- };
.cpp中:
- bool HelloWorld::init()
- {
-
-
- if ( !CCLayer::init() )
- {
- return false;
- }
-
-
- CCSize visibSize=CCDirector::sharedDirector()->getVisibleSize();
-
- CCTableView *tableView=CCTableView::create(this, CCSizeMake(visibSize.width, visibSize.height));
-
- tableView->setDirection(kCCScrollViewDirectionVertical);
-
- tableView->setPosition(CCPointZero);
-
-
- tableView->setDelegate(this);
-
- tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
-
- this->addChild(tableView,1);
-
- tableView->reloadData();
-
-
- return true;
- }
回调函数:
- unsigned int HelloWorld::numberOfCellsInTableView(CCTableView *table)
- {
-
- return 10;
-
-
- }
-
- CCTableViewCell* HelloWorld::tableCellAtIndex(CCTableView *table, unsigned int idx)
- {
-
- CCLOG("%d",idx);
- CCString *nameString=CCString::createWithFormat("icon_%d.png",idx+1);
-
- CCTableViewCell *cell = table->dequeueCell();
-
- if (!cell) {
-
- cell = new CCTableViewCell();
-
- cell->autorelease();
- CCSprite *bgSprite = CCSprite::create("cell.png");
- bgSprite->setAnchorPoint(CCPointZero);
- bgSprite->setPosition(CCPointZero);
- bgSprite->setTag(789);
- cell->addChild(bgSprite);
-
-
- CCSprite *iconSprite = CCSprite::create(nameString->getCString());
- iconSprite->setScale(0.6);
- iconSprite->setAnchorPoint(CCPointZero);
- iconSprite->setPosition(ccp(25, 10));
- iconSprite->setTag(123);
- cell->addChild(iconSprite);
-
-
- CCLabelTTF *pLabel = CCLabelTTF::create(nameString->getCString(), "Arial", 30.0);
- pLabel->setPosition(ccp(200, 10));
- pLabel->setTag(456);
- pLabel->setAnchorPoint(CCPointZero);
- cell->addChild(pLabel);
- }
- else
- {
-
-
- CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage(nameString->getCString());
-
- CCSprite *pSprite=(CCSprite *)cell->getChildByTag(123);
-
- pSprite->setTexture(aTexture);
-
-
- CCLabelTTF *pLabel = (CCLabelTTF*)cell->getChildByTag(456);
- pLabel->setString(nameString->getCString());
-
-
-
-
-
- }
-
-
- return cell;
-
-
- }
-
-
- CCSize HelloWorld::cellSizeForTable(CCTableView *table)
- {
-
-
- CCSize visibSize=CCDirector::sharedDirector()->getVisibleSize();
-
- return CCSizeMake(visibSize.width, 84);
-
-
- }
-
- void HelloWorld::tableCellHighlight(CCTableView *table, CCTableViewCell *cell)
- {
-
- CCLOG("wwww");
-
- CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("cell_selected.png");
-
- CCSprite *pSprite=(CCSprite *)cell->getChildByTag(789);
-
- pSprite->setTexture(aTexture);
-
-
- }
-
- void HelloWorld::tableCellUnhighlight(CCTableView *table, CCTableViewCell *cell)
- {
-
-
- CCLOG("dddd");
-
-
- CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("cell.png");
-
- CCSprite *pSprite=(CCSprite *)cell->getChildByTag(789);
-
- pSprite->setTexture(aTexture);
-
-
-
- }
-
-
- void HelloWorld::tableCellTouched(CCTableView *table, CCTableViewCell *cell)
- {
-
-
- CCLog("cell touched at index: %i", cell->getIdx());
-
-
- }
OK,我们看下效果图:
一个未选择状态,一个选中状态,注意,tableCellHighlight,tableCellUnhighlight,tableCellTouched的执行先后顺序为tableCellHighlight->tableCellUnhighlight->tableCellTouched。OK,就这么简单,貌似cctableview没有自带的滚动条,可能需要自己来实现,希望以后的版本中直接加入滚动条,这样会更方便~~~