在网上找了一些又关TableView的demo,但是这些代码都是预先缓存cell的内容,但是我现在想动态的添加删除cell,该怎么去操作呢?
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_);
//添加列表
m_lpTableView = TableView::create(this, Size(bg_->getContentSize().width, bg_->getContentSize().height - 40));
m_lpTableView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
m_lpTableView->setAnchorPoint(Vec2(0, 0));
m_lpTableView->setDelegate(this);
m_lpTableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
bg_->addChild(m_lpTableView,1);
m_lpTableView->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 4;
}
//生成cell
TableViewCell* TableViewLayer::tableCellAtIndex(TableView *table, ssize_t idx)
{
char szNameString[128];
sprintf(szNameString, "change_%d.png", idx);
m_lpCell = table->dequeueCell();
if (!m_lpCell)
{
m_lpCell = new TableViewCell();
m_lpCell->autorelease();
//设置当前cell图片
//Sprite *iconSprite = Sprite::create(szNameString);
LabelTTF *iconSprite = LabelTTF::create(szNameString, "微软雅黑", 12);
iconSprite->setAnchorPoint(Vec2::ZERO);
iconSprite->setPosition(Vec2(0, 0));
iconSprite->setColor(Color3B(250,250,250));
iconSprite->setTag(1);
m_lpCell->addChild(iconSprite);
//创建一个标签
LabelTTF *label = LabelTTF::create(szNameString, "微软雅黑", 12);
//设置标签相对cell的位置
label->setPosition(Vec2(95, 0));
//设置标签锚点为左下角
label->setAnchorPoint(Vec2::ZERO);
//为标签做一个标记,以便于在cell在重用队列中被取出来时,能够获取的该label并重置label信息
label->setTag(2);
//将标签加入到cell中
m_lpCell->addChild(label);
}
else
{
//创建了就不需要再重新创建了,不然你会发现图片跟文字都不对
//Texture2D *aTexture = TextureCache::sharedTextureCache()->addImage(szNameString);
LabelTTF *pSprite = (LabelTTF *)m_lpCell->getChildByTag(1);
pSprite->setString(szNameString);
LabelTTF *label = (LabelTTF *)m_lpCell->getChildByTag(2);
label->setString(szNameString);
}
return m_lpCell;
}
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)
{
}
void TableViewLayer::insertCellAtIndex(ssize_t idx)
{
char szNameString[128];
sprintf(szNameString, "change_%d.png", idx);
TableViewCell *cell = new TableViewCell();
cell->autorelease();
//设置当前cell图片
//Sprite *iconSprite = Sprite::create(szNameString);
LabelTTF *iconSprite = LabelTTF::create(szNameString, "微软雅黑", 12);
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);
}