Help with TableView cell selection

I tried TableView 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 and it gives nullptr, henceforth I am not able to change the sprite back to unselected.png

Does anyone know any way around… ??

If you request cell that is not visible at the moment you will receive nullptr. This is how UITableView works in Cocoa Touch. So you have to create such an architecture that you will never need to get cell itself. Why do you need a cell? You have everything in your data source.