首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cocos2d-x中tableview的单元接触误操作

cocos2d-x中tableview的单元接触误操作
EN

Stack Overflow用户
提问于 2016-05-18 04:19:50
回答 1查看 329关注 0票数 1

我想这样做:当用户一次触摸一个单元格,一个苹果出现,并且触摸同一个单元格两次,苹果就消失了。但如果我触摸一个细胞0,苹果不仅出现在触碰的细胞上,而且还出现在细胞8,细胞16上。同样的情况发生在细胞1,9,17细胞2,10,18上。当再次接触细胞时,苹果同时消失。不知道为什么会这样。

我用Shubbank的答案解决了这个问题。

代码语言:javascript
复制
// 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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-18 04:29:21

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

代码语言:javascript
复制
//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方法中,将可见性从模式更改为

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37290242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档