cocos2d-x3.0RC2 TableViewDemo
HelloWorldScene.h
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
class HelloWorld : public cocos2d::Layer , public TableViewDataSource, public TableViewDelegate
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx);
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx);
virtual ssize_t numberOfCellsInTableView(TableView *table);
virtual void tableCellTouched(TableView* table, TableViewCell* cell);
void scrollViewDidScroll(cocos2d::extension::ScrollView *view);
void scrollViewDidZoom(cocos2d::extension::ScrollView *view);
};
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
}
Size size = Director::getInstance()->getVisibleSize();
TableView* tableView = TableView::create(this, Size(200, 400));
tableView->setAnchorPoint(Point(0.5f, 0.5f));
tableView->setPosition(Point(size.width / 2, 0));
tableView->setDirection(ScrollView::Direction::VERTICAL);
tableView->setDelegate(this);
tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
tableView->reloadData();
this->addChild(tableView, 1);
this->setTouchEnabled(true);
return true;
}
Size HelloWorld::tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx)
{
return Size(600, 100);
}
TableViewCell* HelloWorld::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx)
{
TableViewCell* cell = table->dequeueCell();
if (!cell) {
cell = new TableViewCell();
cell->autorelease();
}
cell->removeAllChildrenWithCleanup(true);
Sprite* testSprite = Sprite::create("CloseNormal.png");
testSprite->setAnchorPoint(Point(0.5f, 0.5f));
testSprite->setPosition(Point(200, idx * 50 + 100));
Size size = testSprite->getContentSize();
testSprite->setContentSize(Size(100, 100));
cell->addChild(testSprite);
return cell;
}
ssize_t HelloWorld::numberOfCellsInTableView(cocos2d::extension::TableView *table)
{
return 40;
}
void HelloWorld::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell)
{
}
void HelloWorld::scrollViewDidScroll(cocos2d::extension::ScrollView *view)
{
}
void HelloWorld::scrollViewDidZoom(cocos2d::extension::ScrollView *view)
{
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
TIL - 2021.05.13 (Thu)야곰 캠프 방학 중 TIL Notion -> Velog 이전하면서 복습하기! 일단 뷰의 재사용을 위해서 재사용되는 cell에 남아있는 text, image 등을 털어내야 한다 이미 구현되어 있는 prepareForR...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.