tableView又遇到高亮选中状态混乱的问题了

代码如下: 

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函数里,但一直并未解决,

有了解的亲麻烦留言告知一下。


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值