CCTableView: Added a delegate method to inform the implementor that a cell as about to be recycled. CCTableView no longer crashes if a CCTableViewDelegate is not provided.

This commit is contained in:
Ed Bartley 2013-01-23 14:04:49 -05:00
parent 908035bdee
commit 39a39fb9f4
2 changed files with 42 additions and 13 deletions

View File

@ -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<unsigned int>();
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;
}
}

View File

@ -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){};
};