diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp index 1f7fecf3dd..97e7c0196f 100644 --- a/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -56,7 +56,6 @@ bool CCTableView::initWithViewSize(CCSize size, CCNode* container/* = NULL*/) m_pCellsUsed = new CCArrayForObjectSorting(); m_pCellsFreed = new CCArrayForObjectSorting(); m_pIndices = new std::set(); - m_pTableViewDelegate = NULL; m_eVordering = kCCTableViewFillBottomUp; this->setDirection(kCCScrollViewDirectionVertical); @@ -106,6 +105,11 @@ void CCTableView::reloadData() CCARRAY_FOREACH(m_pCellsUsed, pObj) { CCTableViewCell* cell = (CCTableViewCell*)pObj; + + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellWillRecycle(this, cell); + } + m_pCellsFreed->addObject(cell); cell->reset(); if (cell->getParent() == this->getContainer()) @@ -363,6 +367,10 @@ CCTableViewCell* CCTableView::_cellWithIndex(unsigned int cellIndex) void CCTableView::_moveCellOutOfSight(CCTableViewCell *cell) { + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellWillRecycle(this, cell); + } + m_pCellsFreed->addObject(cell); m_pCellsUsed->removeSortedObject(cell); m_pIndices->erase(cell->getIdx()); @@ -387,10 +395,9 @@ void CCTableView::scrollViewDidScroll(CCScrollView* view) { return; } - - if (m_pTableViewDelegate) - { - m_pTableViewDelegate->scrollViewDidScroll(this); + + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->scrollViewDidScroll(this); } unsigned int startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0; @@ -493,9 +500,11 @@ void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) return; } - if (m_pTouchedCell) { - m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); - m_pTableViewDelegate->tableCellTouched(this, m_pTouchedCell); + if (m_pTouchedCell){ + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + m_pTableViewDelegate->tableCellTouched(this, m_pTouchedCell); + } m_pTouchedCell = NULL; } @@ -524,13 +533,16 @@ bool CCTableView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) index = this->_indexFromOffset(point); m_pTouchedCell = this->_cellWithIndex(index); - - if (m_pTouchedCell) { + + if (m_pTouchedCell && m_pTableViewDelegate != NULL) { m_pTableViewDelegate->tableCellHighlight(this, m_pTouchedCell); } } else if(m_pTouchedCell) { - m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + } + m_pTouchedCell = NULL; } @@ -542,7 +554,10 @@ void CCTableView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) CCScrollView::ccTouchMoved(pTouch, pEvent); if (m_pTouchedCell && isTouchMoved()) { - m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + } + m_pTouchedCell = NULL; } } @@ -552,7 +567,10 @@ void CCTableView::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) CCScrollView::ccTouchCancelled(pTouch, pEvent); if (m_pTouchedCell) { - m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + if(m_pTableViewDelegate != NULL) { + m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell); + } + m_pTouchedCell = NULL; } } diff --git a/extensions/GUI/CCScrollView/CCTableView.h b/extensions/GUI/CCScrollView/CCTableView.h index 04853db517..d289ac2957 100644 --- a/extensions/GUI/CCScrollView/CCTableView.h +++ b/extensions/GUI/CCScrollView/CCTableView.h @@ -70,6 +70,17 @@ public: * @param cell cell that is pressed */ virtual void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell){}; + + /** + * Delegate called when the cell is about to be recycled. Immediately + * after this call the cell will be removed from the scene graph and + * recycled. + * + * @param table table contains the given cell + * @param cell cell that is pressed + */ + virtual void tableCellWillRecycle(CCTableView* table, CCTableViewCell* cell){}; + };