runTableViewTest.h
#ifndef __TABLEVIEWTESTSCENE_H__
#define __TABLEVIEWTESTSCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
void runTableViewTest();
class TableViewTestLayer : public cocos2d::CCLayer, publiccocos2d::extension::CCTableViewDataSource, publiccocos2d::extension::CCTableViewDelegate//像OCtableView的使用实现两个代理方法
{
public:
virtual bool init();
void toExtensionsMainLayer(cocos2d::CCObject *sender);
CREATE_FUNC(TableViewTestLayer);
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 // __TABLEVIEWTESTSCENE_H__
runTableViewTest.cpp
#include "TableViewTestScene.h"
#include "CustomTableViewCell.h"
#include "../ExtensionsTest.h"
USING_NS_CC;
USING_NS_CC_EXT;
void runTableViewTest()
{
CCScene *pScene = CCScene::create();
TableViewTestLayer *pLayer = TableViewTestLayer::create();
pScene->addChild(pLayer);
CCDirector::sharedDirector()->replaceScene(pScene);
}
// on "init" you need to initialize your instance
bool TableViewTestLayer::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCTableView* tableView = CCTableView::create(this, CCSizeMake(60, 280));
tableView->setDirection(kCCScrollViewDirectionVertical);//方向
tableView->setPosition(ccp(winSize.width-150,winSize.height/2-120));
tableView->setDelegate(this);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);//设置table加载的位置 如1,2,3 还是3,2,1
this->addChild(tableView);
tableView->reloadData();
// Back Menu
CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", this,menu_selector(TableViewTestLayer::toExtensionsMainLayer));
itemBack->setPosition(ccp(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
CCMenu *menuBack = CCMenu::create(itemBack, NULL);
menuBack->setPosition(CCPointZero);
addChild(menuBack);
return true;
}
void TableViewTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender)
{
ExtensionsTestScene *pScene = new ExtensionsTestScene();
pScene->runThisTest();
pScene->release();
}
//点击table行数的响应
void TableViewTestLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLOG("cell touched at index: %i", cell->getIdx());
}
//设置table每行的宽高
CCSize TableViewTestLayer::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(60, 60);
}
//对table的每行的加载
CCTableViewCell* TableViewTestLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
CCString *string = CCString::createWithFormat("%d", idx);
CCTableViewCell *cell = table->dequeueCell();
if (!cell) {
cell = new CustomTableViewCell();
cell->autorelease();
CCSprite *sprite = CCSprite::create("Images/Icon.png");
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(ccp(0, 0));
cell->addChild(sprite);
CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
label->setPosition(CCPointZero);
label->setAnchorPoint(CCPointZero);
label->setTag(123);
cell->addChild(label);
}
else
{
CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);
label->setString(string->getCString());
}
return cell;
}
//设置table的行数
unsigned int TableViewTestLayer::numberOfCellsInTableView(CCTableView *table)
{
return 20;
}