2012-09-09 22:34:32 +08:00
|
|
|
#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();
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
CCTableView* tableView = CCTableView::create(this, CCSizeMake(250, 60));
|
|
|
|
tableView->setDirection(kCCScrollViewDirectionHorizontal);
|
2012-09-09 22:34:32 +08:00
|
|
|
tableView->setPosition(ccp(20,winSize.height/2-30));
|
|
|
|
tableView->setDelegate(this);
|
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
tableView = CCTableView::create(this, CCSizeMake(60, 280));
|
|
|
|
tableView->setDirection(kCCScrollViewDirectionVertical);
|
2012-09-09 22:34:32 +08:00
|
|
|
tableView->setPosition(ccp(winSize.width-150,winSize.height/2-120));
|
|
|
|
tableView->setDelegate(this);
|
2012-09-10 13:39:12 +08:00
|
|
|
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
|
|
|
|
|
|
|
// Back Menu
|
|
|
|
CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", this, menu_selector(TableViewTestLayer::toExtensionsMainLayer));
|
|
|
|
itemBack->setPosition(ccp(winSize.width - 50, 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();
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
void TableViewTestLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
CCLOG("cell touched at index: %i", cell->getIdx());
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize TableViewTestLayer::cellSizeForTable(CCTableView *table)
|
|
|
|
{
|
|
|
|
return CCSizeMake(60, 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int TableViewTestLayer::numberOfCellsInTableView(CCTableView *table)
|
|
|
|
{
|
|
|
|
return 20;
|
|
|
|
}
|