代码如下:
bool HelloWorld::init(){
CCTableView* tableView = CCTableView::create(this, CCSizeMake(450, 420));
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setAnchorPoint(ccp(0, 1));
tableView->setVerticalFillOrder(CCTableViewVerticalFillOrder::kCCTableViewFillTopDown);
tableView->setTag(124);
tableView->setPosition(winSize.width - 460, 50);
addChild(tableView);
tableView->setDelegate(this);
tableView->reloadData();
}
CCSize HelloWorld::tableCellSizeForIndex(cocos2d::extension::CCTableView *table, unsigned int idx)
{
return CCSizeMake(450, 55);
}
CCTableViewCell* HelloWorld::tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx)
{
CCString *string = CCString::createWithFormat("%d", idx + 1);
CCTableViewCell *cell = table->dequeueCell();
if (!cell) {
cell = new CCTableViewCell();
cell->autorelease();
CCScale9Sprite *sprite = CCScale9Sprite::createWithSpriteFrameName("rank_fg1.png");
sprite->setContentSize(CCSize(450, 55));
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(ccp(0, 0));
cell->addChild(sprite);
sprite->setTag(123);
CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
label->setPosition(CCPointZero);
label->setAnchorPoint(CCPointZero);
label->setTag(124);
cell->addChild(label, 1);
}
else
{
CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(124);
label->setString(string->getCString());
CCScale9Sprite* temp = (CCScale9Sprite*)cell->getChildByTag(125);
if (temp)
{
CCLOG("chu xian le %d", idx + 1);//天坑
}
}
return cell;
}
unsigned int HelloWorld::numberOfCellsInTableView(cocos2d::extension::CCTableView *table)
{
return 20;
}
void HelloWorld::tableCellHighlight(CCTableView * table,CCTableViewCell * cell)
{
/*CCScale9Sprite* sp = (CCScale9Sprite*)cell->getChildByTag(123);
if (sp)
{
CCScale9Sprite* temp = CCScale9Sprite::createWithSpriteFrameName("rank_fg3.png");
temp->setContentSize(CCSize(450, 55));
temp->setAnchorPoint(CCPointZero);
temp->setPosition(ccp(0, 0));
cell->addChild(temp);
temp->setTag(125);
}*/
}
void HelloWorld::tableCellUnhighlight(CCTableView * table,CCTableViewCell * cell)
{
//cell->removeChildByTag(125);
}
void HelloWorld::tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell)
{
for (int i = 0 ; i < 20; i++)
{
CCTableViewCell* pCell = table->cellAtIndex(i);
if (pCell)
{
pCell->removeChildByTag(125);
}
}
CCString* _str = CCString::createWithFormat("cell touched at index: %d", cell->getIdx() + 1);
scrollCallBack(_str->getCString());
CCScale9Sprite* temp = CCScale9Sprite::createWithSpriteFrameName("rank_fg3.png");
temp->setContentSize(CCSize(450, 55));
temp->setAnchorPoint(CCPointZero);
temp->setPosition(ccp(0, 0));
cell->addChild(temp);
temp->setTag(125);
}
为了简洁,我这边只粘了部分代码,但已能说明问题。遇到问题如下:
当点击第一个cell时:
但是当我滑动时发现第十个cell也被选中了,
于是,我在tableCellTouched函数设置断点调试了下,发现tableCellTouched函数只被调用了一次,于是猜测问题可能出现在tableCellAtIndex函数里,但一直并未解决,
有了解的亲麻烦留言告知一下。