Most efficient way to use CCTableView with background images in cells

I am currently learning cocos2d-x, and I am attempting to make a navigation menu (ListView-like), with each cell having a different image (and text, obviously). I have looked at CCTableView and as the successor to CCListView it seems to be able to do the job.

I have inspired myself from the TableViewTest project in the distribution, but I can’t quite get it to work (SIGSEGV and such). The problem is probably how I handle images.

I think I understand that CCTableView only keeps content for the visible cells, and that as the user scrolls, the cells are “dequeued”, and made available for other cells that come into view. So that cells need to be updated in the CCTableView::tableCellAtIndex function. Since I want to keep memory usage at a minimum. I don’t want to continually keep creating CCSprite objects. So I load the background PNGs into the CCTextureCache and use that. Is this the best way?

This is the relevant code (NOTE: I am testing, so it is not pretty, yet)

For testing purposes, I use a static array to hold the PNG names. MenuList and MenuCell are the direct equivalents from the TableViewTest project (MenuList is derived from CCTableView, MenuCell just has draw)

Can you see anything wrong with this or that can make it more efficient?

#include "MenuList.h"
#include "MenuCell.h"
#include "VisibleRect.h"

USING_NS_CC;
USING_NS_CC_EXT;

const char* imageStr[] = { "ui_buttons_blue.png", "ui_buttons_blue2.png", "ui_buttons_blue3.png",
 "ui_buttons_green.png", "ui_buttons_green2.png", "ui_buttons_mask.png", "ui_buttons_orange.png", 
"ui_buttons_orange2.png", "ui_buttons_pink.png", "ui_buttons_purple.png", "ui_buttons_red.png", 
"ui_buttons_red2.png", "ui_buttons_red3.png", "ui_buttons_red4.png", "ui_buttons_violet.png", 
"ui_buttons_yellow.png", "ui_buttons_yellow2.png" };

int imageStrCount = sizeof(imageStr)/sizeof(imageStr[0]);

bool MenuList::init() {
    if ( !CCLayer::init() ) {
        return false;
    }

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCTableView* tableView = CCTableView::create(this, CCSizeMake(200, 280));
    tableView->setDirection(kCCScrollViewDirectionVertical);
    tableView->setPosition(ccp(20,winSize.height/2-30));
    tableView->setDelegate(this);
    tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
    this->addChild(tableView);
    tableView->reloadData();

    return true;
}

void MenuList::tableCellTouched(CCTableView* table, CCTableViewCell* cell) {
    CCLOG("cell touched at index: %i", cell->getIdx());
}

CCSize MenuList::cellSizeForTable(CCTableView *table) {
    return CCSizeMake(200, 60);
}

CCTableViewCell* MenuList::tableCellAtIndex(CCTableView *table, unsigned int idx) {

    CCString *string = CCString::createWithFormat("%d", idx);
    CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage(imageStr[idx%imageStrCount]);

    CCTableViewCell *cell = table->dequeueCell();   
    CCRect rect(0.0f, 0.0f, 200.0f, 60.0f);

    CCLOG("IDX = %d", idx);

    if (!cell) {
        cell = new MenuCell();
        cell->autorelease();

        CCLOG("CELL NULL IDX = %d", idx);

        CCSprite *sprite = CCSprite::createWithTexture(texture,rect);
        sprite->setAnchorPoint(CCPointZero);
        sprite->setPosition(ccp(0, 0));
        sprite->setTag(456);
        cell->addChild(sprite);        

        CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
        label->setColor(ccBLACK);
        label->setPosition(CCPointZero);
        label->setAnchorPoint(CCPointZero);
        label->setTag(123);
        cell->addChild(label);

    } else {
        CCSprite *sprite = (CCSprite *)cell->getChildByTag(456);
        sprite->setTexture(texture);

        CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);
        label->setString(string->getCString());
    }

    return cell;
}

unsigned int MenuList::numberOfCellsInTableView(CCTableView *table) {
    return 10;
}

EDIT: I had an autorelease on the created sprite, which I removed. It solved my SIGSEGV issue, but is this still the way to do it?