cocos2dx UITableView 使用

本文转自:http://blog.csdn.net/qq_22203741/article/details/77678291

在这篇文章中,我们将讲下CCTableView,顾名思义,CCTableView表试图,在iOS里是tableview,Android里是listview,都是差不多的意思,在cocos2dx中CCTableView继承自CCScrollview。OK,下面我们直接上代码:
在头文件.h中:
class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,cocos2d::extension::CCTableViewDataSource
{
public:
// Method ‘init’ in cocos2d-x returns bool, instead of ‘id’ in cocos2d-iphone (an object pointer)
virtual bool init();

// there's no 'id' in cpp, so we recommend to return the class instance pointer  
static cocos2d::CCScene* scene();  

// a selector callback  
void menuCloseCallback(CCObject* pSender);  

// preprocessor macro for "static create()" constructor ( node() deprecated )  
CREATE_FUNC(HelloWorld);  

public:

//CCTableViewDelegate继承自CCScrollViewDelegate  
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);  

virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view);  

//点击哪个cell  
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);  
//每个cell的size  
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);  
//生成cell  
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);  
//cell的数量  
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()
{
//
// 1. super init first
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->setAnchorPoint(ccp(0, 0));
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());  



   // this->scrollBar(table);  

}  


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());

}

效果图:

你可能感兴趣的:(cocos2d-x)