肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026
有一些人会问到CCTableView如何使用以及使用时出现得一些问题, 今天我为大家介绍一下,大家先看下效果
为了方便大家观看,我新建一个工程;
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace extension;
class HelloWorld : public cocos2d::CCLayer, public CCTableViewDataSource, public CCTableViewDelegate
{
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:
void scrollViewDidScroll(CCScrollView* view);
void scrollViewDidZoom(CCScrollView* view);
public:
unsigned int numberOfCellsInTableView(CCTableView *table);
CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);
CCSize cellSizeForTable(CCTableView *table);
CCSize tableCellSizeForIndex(CCTableView *table, unsigned int idx);
public:
void tableCellTouched(CCTableView* table, CCTableViewCell* cell);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("By Cooki", "Thonburi", 40);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) );
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
CCTableView * tableView = CCTableView::create(this, CCSizeMake(480, 260));
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setPosition(ccp(0, 10));
tableView->setDelegate(this);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(tableView);
return true;
}
#pragma mark - CCTableViewDataSource
unsigned int HelloWorld::numberOfCellsInTableView(CCTableView *table) {
return 8;
}
CCTableViewCell* HelloWorld::tableCellAtIndex(CCTableView *table, unsigned int idx) {
CCTableViewCell* cell = table->cellAtIndex(idx);
if (!cell) {
cell = new CCTableViewCell();
cell->autorelease();
CCSprite* item = CCSprite::create("market_item_2.png");
item->setPosition(ccp(240,28));
cell->addChild(item);
}
return cell;
}
CCSize HelloWorld::cellSizeForTable(CCTableView *table) {
return CCSizeMake(480, 51);
}
CCSize HelloWorld::tableCellSizeForIndex(CCTableView *table, unsigned int idx){
// if (idx == (isOpen - 1)) {
// return CCSizeMake(960, 162);
// }
// else
return CCSizeMake(480, 56);
}
#pragma mark - CCTableViewDelegate
void HelloWorld::tableCellTouched(CCTableView* table, CCTableViewCell* cell) {
}
#pragma mark - CCScrollViewDelegate
void HelloWorld::scrollViewDidScroll(CCScrollView* view) {
}
void HelloWorld::scrollViewDidZoom(CCScrollView* view) {
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
效果如图:
至于实现协议函数得作用大家可以打个日志看下比如:
void HelloWorld::tableCellTouched(CCTableView* table, CCTableViewCell* cell) {
CCLog("you touch cell at %d", cell->getIdx());
}