我照这网上写了一个滚动条动能, 但是cell的内容都是预先写死的,怎么去动态增加和减少cell呢?
#include “TableViewLayer.h”
using namespace cocos2d;
using namespace CocosDenshion;
TableViewLayer::TableViewLayer()
{
bool bRet = false;
bRet = init();
autorelease();
}
TableViewLayer::~TableViewLayer()
{
}
bool TableViewLayer::init()
{
if (!Layer::init())
{
return false;
}
//获取屏幕大小
Size visibSize = Director::sharedDirector()->getVisibleSize();
//设置背景
Sprite *bg_ = Sprite::create("pic_InfoBg.png");
this->addChild(bg_);
//添加列表
TableView *tableView = TableView::create(this, Size(bg_->getContentSize().width, bg_->getContentSize().height - 40));
tableView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
tableView->setAnchorPoint(Vec2(0, 0));
tableView->setDelegate(this);
tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
bg_->addChild(tableView,1);
tableView->reloadData();
return true;
}
void TableViewLayer::menuCloseCallback(Object* pSender)
{
Director::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//cell的数量
ssize_t TableViewLayer::numberOfCellsInTableView(TableView *table)
{
return 3;
}
//生成cell
TableViewCell* TableViewLayer::tableCellAtIndex(TableView *table, ssize_t idx)
{
char szNameString;
sprintf(szNameString, “change_%d.png”, idx);
TableViewCell *cell = table->dequeueCell();
if (!cell)
{
cell = new TableViewCell();
cell->autorelease();
//设置当前cell图片
Sprite *iconSprite = Sprite::create(szNameString);
iconSprite->setScale(0.5);
iconSprite->setAnchorPoint(Vec2::ZERO);
iconSprite->setPosition(Vec2(0, 0));
iconSprite->setTag(123);
cell->addChild(iconSprite);
//创建一个标签
LabelTTF *label = LabelTTF::create(szNameString, "微软雅黑", 12);
//设置标签相对cell的位置
label->setPosition(Vec2(95, 0));
//设置标签锚点为左下角
label->setAnchorPoint(Vec2::ZERO);
//为标签做一个标记,以便于在cell在重用队列中被取出来时,能够获取的该label并重置label信息
//label->setTag(123);
//将标签加入到cell中
cell->addChild(label);
}
else
{
//创建了就不需要再重新创建了,不然你会发现图片跟文字都不对
Texture2D *aTexture = TextureCache::sharedTextureCache()->addImage(szNameString);
Sprite *pSprite = (Sprite *)cell->getChildByTag(123);
pSprite->setTexture(aTexture);
}
return cell;
}
Size TableViewLayer::cellSizeForTable(TableView *table)
{
return Size(50, 40);
}
void TableViewLayer::tableCellHighlight(TableView *table, TableViewCell *cell)
{
}
void TableViewLayer::tableCellUnhighlight(TableView *table, TableViewCell *cell)
{
}
void TableViewLayer::tableCellTouched(TableView *table, TableViewCell *cell)
{
Blink *blink_ = Blink::create(1.0f, 7);
cell->runAction(blink_);
}
void TableViewLayer::scrollBar(TableView* table)
{
}