我想这样做:当用户一次触摸一个单元格,一个苹果出现,并且触摸同一个单元格两次,苹果就消失了。但如果我触摸一个细胞0,苹果不仅出现在触碰的细胞上,而且还出现在细胞8,细胞16上。同样的情况发生在细胞1,9,17细胞2,10,18上。当再次接触细胞时,苹果同时消失。不知道为什么会这样。
我用Shubbank的答案解决了这个问题。
// I declare this on HelloWorld.h
int myArray[20];
// and this part is HelloWorld.cpp
// other codes..
void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell)
{
log("cell number : %d", cell->getIdx());
// i inserted for test. all cell have different idx number.
//add to the array if not found.
if (myArray[cell->getIdx()] == 0) {
myArray[cell->getIdx()] = 1;
}
else {
myArray[cell->getIdx()] = 0;
}
auto touchedCell = cell->getChildByTag(2);
if (myArray[cell->getIdx()] == 0)
{
touchedCell->setVisible(false);
}
else {
touchedCell->setVisible(true);
}
}
Size HelloWorld::tableCellSizeForIndex(TableView* table, ssize_t idx)
{
return Size(100, 90);
}
TableViewCell* HelloWorld::tableCellAtIndex(TableView* table, ssize_t idx)
{
auto string = String::createWithFormat("%ld", idx);
TableViewCell* cell = table->dequeueCell();
if (cell == false)
{
cell = new CustomTableViewCell();
cell->autorelease();
auto apple = Sprite::create("apple.png");
apple->setAnchorPoint(Point::ZERO);
apple->setPosition(Point::ZERO);
apple->setTag(2);
apple->setVisible(false);
cell->addChild(apple);
auto label = LabelTTF::create(string->getCString(), "arial", 20.0);
label->setAnchorPoint(Point::ZERO);
label->setPosition(Point(5, 5));
label->setTag(120);
cell->addChild(label);
}
else {
auto label = (LabelTTF*)cell->getChildByTag(120);
label->setString(string->getCString());
}
auto touchedCell = cell->getChildByTag(2);
if (myArray[cell->getIdx()] == 0)
{
touchedCell->setVisible(false);
}
else {
touchedCell->setVisible(true);
}
return cell;
}
ssize_t HelloWorld::numberOfCellsInTableView(TableView* table)
{
return 20;
}发布于 2016-05-18 04:29:21
您需要将单元格选择存储在模式中,而不是直接操作单元格。
//declare a int[20] array in .h file
void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell)
{
log("cell number : %d", cell->getIdx());
// i inserted for test. all cell have different idx number.
//add to the array if not found.
if (myArray[cell->getIdx()] == 0) {
myArray[cell->getIdx()] = 1
}
else {
myArray[cell->getIdx()] = 0
}
auto touchedCell = cell->getChildByTag(2);
if (myArray[cell->getIdx()] == 0)
{
touchedCell->setVisible(false);
}
else {
touchedCell->setVisible(true);
}
}现在,在您的tableCellAtIndex方法中,将可见性从模式更改为
TableViewCell* HelloWorld::tableCellAtIndex(TableView* table, ssize_t idx) {
// other code
auto touchedCell = cell->getChildByTag(2);
if (myArray[cell->getIdx()] == 0)
{
touchedCell->setVisible(false);
}
else {
touchedCell->setVisible(true);
}
return cell;
}https://stackoverflow.com/questions/37290242
复制相似问题