[C++] Find a child inside CCTableView recursively

[C++] Find a child inside CCTableView recursively
0.0 0


Hi All,
I have a running scene which is my home page. There is a table view (with tag MY_TABLEVIEW_TAG) inside it. It contains two cells. First cell is large which contains one item only and other is also large but contains 10 first level items. There is a first level item in second cell which contains a node with tag MY_NODE_TO_FIND. I need to find that and remove that in some scenario. For that I wrote a recursive function which returns the node with

I wrote a function to find it:

#define MY_TABLEVIEW_TAG 1003
#define MY_NODE_TO_FIND 2009
CCNode* getChildByTagRecursive(CCNode* node, int tag){
    if (node->getChildByTag(tag) != NULL) {
        return node;
    auto array = node->getChildren();
    CCObject* temp;
    CCARRAY_FOREACH(array, temp){
        CCNode* t = (CCNode*)temp;
        return getChildByTagRecursive(t, tag);
    return NULL;

There are many nested items are on the page. With this function I’m able to find table view with the provided tag like:

void searchNode(){
    CCScene* runningScene = CCDirector::sharedDirector()->getRunningScene();
    CCNode* n = getChildByTagRecursive(runningScene, MY_TABLEVIEW_TAG); //SUCCESS
    //CCNode* n = getChildByTagRecursive(runningScene, MY_NODE_TO_FIND); //FAILURE

    if(n != NULL) {
        CCTableView* tv = (CCTableView*)n;
        CCNode* mynode = getChildByTagRecursive(tv, MY_NODE_TO_FIND); // FAILURE: unable to find MY_NODE_TO_FIND, Able to find some of the nodes but all.
        if (mynode != NULL) {
            CCLog("--- FOUND MYNODE ---");
        } else {
            CCLog("COULDN'T FIND MYNODE ---");
    } else {

I tried everything (working past 6-7 hours) but bailed to find my node.
I’m not sure if there is anything wrong withe the function “getChildByTagRecursive”. Or CCTtableView is using different data structure.
I guess, most probably there is a problem with my search method.

Can anybody help??
Thanks in advance.


Here’s what I do for recursive find. It will return the node or NULL if not found.

    static CCNode *getChildByTagRecursive(const int &nodeTag, CCNode *parent) {
        CCNode *nodeFound = parent->getChildByTag(nodeTag);
        if (!nodeFound) {
            CCArray *children = parent->getChildren();
            CCARRAY_FOREACH_VALUE(children, CCNode*, child) {
                nodeFound = getChildByTagRecursive(nodeTag, child);
                if (nodeFound) break;
        return nodeFound;

It’s using this macro, just because I’m a bit lazy to dynamic cast from CCObject all the time ; )

#define CCARRAY_FOREACH_VALUE(__array__, __class__, __value__)                                                          \
    __class__ __value__;                                                                                                \
    if ((__array__) && (__array__)->data->num > 0)                                                                      \
        for(CCObject** __arr__ = (__array__)->data->arr, **__end__ = (__array__)->data->arr + (__array__)->data->num-1; \
                __arr__ <= __end__ && (((__value__) = dynamic_cast<__class__>(*__arr__)) != NULL);                      \


Thank you so much lajos, Its working great. Have a nice day :slight_smile:


Here you should return node->getChildByTag(tag); not node :wink:


Here is a newer version:

    static Node *getChildByTagRecursive(const int &nodeTag, Node *parent) {
    Node *nodeFound = parent->getChildByTag(nodeTag);
    if (!nodeFound) {
        auto children = parent->getChildren();
        for(auto child: children){
            nodeFound = getChildByTagRecursive(nodeTag, child);
            if (nodeFound) break;
    return nodeFound;

called like this:

cocos2d::ui::Button* signupButton = static_cast<cocos2d::ui::Button*>(getChildByTagRecursive(_sceneElements.signupTag, this));