cocos2d-x中tableview的cell touch误操作

cell touch misoperation of tableview in cocos2d-x

我想做成这样:当用户触摸一个单元格一次,一个苹果出现,然后触摸同一个单元格两次,苹果消失。但是如果我触摸单元格 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;
}

您需要将单元格选择存储在模式中,而不是直接操作单元格。

//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;
}