Help with TableView/ ScrollView

  • pabitrapadhy
    Likes 0

    Problem Description

    Hello Everyone,

    I am currently working on a game where, I want to display player profiles in some kind of ScrollView/ TableView.
    I am a little confused with all the tutorials. I just want to know the correct way of doing it.

    Attached is the image, of somewhat similiar that I am trying to achieve.
    http://techbirds.in/how-to-use-scrollview-in-cocos2d-x/

    I need a clear and concise method of doing this.
    I know sonar system’s methods are right to point and clear.
    Hope they could help.

    Regards

  • Sonar Systems admin
    Likes 0

    Try there method and let us know how it goes.

  • pabitrapadhy
    Likes 0

    I tried it and it seems to work as expected, and I have managed to dynamically create cells with the user profiles from json.

    Apart from that I have some issues.
     

    void PMHomeScene::tableCellTouched(TableView* table, TableViewCell* cell)
    {
        CCLOG("cell touched at index: %ld", cell->getIdx());
        
        CCLOG("old selected index is %d", UserDefault::getInstance()->getIntegerForKey("selected_index"));
        // TODO: get child by tag for cell
        //TODO: change the selected index sprite
        
        if(UserDefault::getInstance()->getIntegerForKey("selected_index") != cell->getIdx())
        {
            auto selectedIndex = UserDefault::getInstance()->getIntegerForKey("selected_index");
            auto oldCell = table->cellAtIndex(selectedIndex);
            auto oldSelectedSprite = static_cast<Sprite *>(oldCell->getChildByTag(121));
            oldSelectedSprite->setTexture("PMHomeScene/profile_btn.png");
            
            // touched cell sprite
            auto sprite = static_cast<Sprite *>(cell->getChildByTag(121));
            sprite->setTexture("PMHomeScene/profile_btn_selected.png");
            UserDefault::getInstance()->setIntegerForKey("selected_index", (int)cell->getIdx());
            CCLOG("new selected index is %d", UserDefault::getInstance()->getIntegerForKey("selected_index"));
    
        }
        
    }

    I am storing a selected index in user default to select a player profile and that will load the game data.
    But, say when I am selecting another profile, then I am scrolling down to the last profile and changing it’s sprite to selected png
    consider, the previously selected index is 0 and the new selected index is 7 and the view is only able to show 4 cells at a time.
    so, as per the logic, the unused cells will be released, so I am not able to get the previously selected cell, henceforth I am not able to change the sprite back to unselected.

    Does anyone know any way around.. ??

  • Sonar Systems admin
    Likes 0

    Don’t quite understand that, could you explain using step by step bullet points please.

Login to reply