先看代码:
.h文件中:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class HelloWorld : public CCLayer,publicCCTableViewDataSource,publicCCTableViewDelegate
{
public:
virtual bool init();
static CCScene* scene();
void menuCloseCallback(CCObject* pSender);
CREATE_FUNC(HelloWorld);
virtual void scrollViewDidScroll(CCScrollView* view){};
virtual void scrollViewDidZoom(CCScrollView* view){};
virtual void tableCellTouched(CCTableView* table,CCTableViewCell* cell);
virtual CCSize tableCellSizeForIndex(CCTableView *table,unsigned int idx);
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table,unsigned int idx);
virtual unsignedint numberOfCellsInTableView(CCTableView *table);
virtual void tableCellHighlight(CCTableView* table,CCTableViewCell* cell){};
virtual void tableCellUnhighlight(CCTableView* table,CCTableViewCell* cell){};
virtual void tableCellWillRecycle(CCTableView* table,CCTableViewCell* cell){};
};
#endif
.cpp文件中
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCMenuItemImage *pCloseItem =CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width -20, 20) );
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu,1);
CCSize size =CCDirector::sharedDirector()->getWinSize();
CCTableView* tableView = CCTableView::create(this, CCSizeMake(300, 80));
tableView->setDirection(kCCScrollViewDirectionHorizontal);
tableView->setPosition(ccp(0,size.height /2));
tableView->setDelegate(this);
this->addChild(tableView,100);
tableView->reloadData();
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void HelloWorld::tableCellTouched(CCTableView* table,CCTableViewCell* cell){
CCLog("cell touch at index;%i",cell->getIdx());
}
CCSize HelloWorld::tableCellSizeForIndex(CCTableView* table,unsigned int idx){
return CCSizeMake(100,100);
}
CCTableViewCell* HelloWorld::tableCellAtIndex(CCTableView* table,unsigned int idx){
CCLOG("zhanghaibinlog:%d",idx);
CCString *string = CCString::createWithFormat("%d",idx);
CCTableViewCell* cell = table->dequeueCell();
if(!cell){
CCLOG("zhanghaibinlog:new:%d",idx);
cell = new CCTableViewCell();
cell->autorelease();
CCSprite* sprite = CCSprite::create("Icon-72.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 HelloWorld::numberOfCellsInTableView(CCTableView* table){
return 30;
}
现在运行程序,可以看到的log为:
Cocos2d: zhanghaibinlog:29
Cocos2d: zhanghaibinlog:new:29
Cocos2d: zhanghaibinlog:0
Cocos2d: zhanghaibinlog:1
Cocos2d: zhanghaibinlog:new:1
Cocos2d: zhanghaibinlog:2
Cocos2d: zhanghaibinlog:new:2
界面上可以看到3个item
从log可以看出:
程序一开始调用了4次tableCellAtIndex,但是只new了3次。
CCTableViewCell* cell = table->dequeueCell();
这句的作用是返回table队列中的某一项
当idx为29时,队列中还没有内容,所以新建一个。
当idx为0时,队列中有了一个29,所以0复用29
当idx为1、2时,都是新创建的。
当向左滑动一个item时
Cocos2d: zhanghaibinlog:3
Cocos2d: zhanghaibinlog:new:3
说明idx为3时又创建了一个新的cell
当向左再滑动一个item时,可以看到29又出来了
Cocos2d: zhanghaibinlog:4
说明idx为4时使用的是29
再向左滑动一个,看到1出来了
Cocos2d: zhanghaibinlog:5
说明idx为5时使用的是1依此滑动,可以验证6用的是2、7用的是3、8用的29、9用的1、10用的是2、11用的3、12用的29、……、29用的1
综上所述,new出来的一共是4个,分别是index为29、1、2、3。所有cell都是直接或是复用这4个cell
当显示最后三个cell时
继续向左滑动,滑动隐藏一个cell然后松手时
zhanghaibinlog:26
隐藏两个时松手时
zhanghaibinlog:27
zhanghaibinlog:26
隐藏三个松手时:
zhanghaibinlog:28
zhanghaibinlog:27
zhanghaibinlog:26
说明也调用了tableCellAtIndex
所以说如果希望在重用的cell中依照idx从新设置新的内容,需要把else中的语句:
// CCLabelTTF* label = (CCLabelTTF*)cell->getChildByTag(123);
// label->setString(string->getCString());
解注释