Search code examples
c++cocos2d-x

How to dequeueCell in a CCTableView with multiple custom CCTableViewCell’s?


I have a CCTableView that i would like to use multiple custom CCTableViewCells, but it seems that dequeueCell treats all cells as if they were the same. The other option is to have a different cell for each row but this is not performant.

Oh and this is cocos2dx v2.2


Solution

  • I actually came up with a decent solution but it requires adding this member to CCTableView

    CCTableViewCell *CCTableView::dequeueCellWithTag(int nTag)
    {
        CCTableViewCell *cell = NULL;
    
        for (int i = 0, len = m_pCellsFreed->count(); i < len; i++) {
            CCTableViewCell *tempCell = (CCTableViewCell*)m_pCellsFreed->objectAtIndex(i);
            if (tempCell->getTag() == nTag) {
                cell = tempCell;
                cell->retain();
                m_pCellsFreed->removeObjectAtIndex(i);
                cell->autorelease();
                break;
            }
        }
    
        return cell;
    }
    

    Basically you do cell->setTag(tagId) on your cell after you create it and then you tableView->dequeueCellWithTag(tagId). Hopefully this could be built in because its pretty standard.

    If anyone knows a better way i'm open to suggestions!