Cocos2d-x 中CCTableView and CCTableViewCell點擊空白區域依然會響應單元格事件及reloadData()問題

轉自:

http://blog.csdn.net/playddt/article/details/8707703

http://blog.sina.com.cn/s/blog_4458fdda0101hvsb.html

(1)在CCTableView中點擊空白區域依然會響應單元格事件,

在CCTableView.cpp中修改


從點擊座標計算點擊單元格的時候,原有的_indexFromOffset自動把返回的值重定到0~size-1範圍內,所以無論點到控件內部的任何位置都會彈出一個單元格事件。

改法是:

1.增加一個方法:(原有的方法的另一個bug是-cell_size~0的範圍和0~cell_size算出來的index都是0)

int CCTableView::indexOfTouch(CCPoint offset){

   const CCSize cellSize = m_pDataSource->cellSizeForTable(this);

   if (m_eVordering == kCCTableViewFillTopDown) {

       offset.y = this->getContainer()->getContentSize().height- offset.y - cellSize.height;

   }

   int  index = 0;

   float __index =0.0;

   switch (this->getDirection()) {

       case kCCScrollViewDirectionHorizontal:

           __index = offset.x/cellSize.width;

           if (__index<0) __index-=1;

           index = __index;

           break;

       default:

           __index = offset.y/cellSize.height;

           if (__index<0) __index-=1;

           index = __index;

           break;

   }

   return index;

}


2. 修改

bool CCTableView::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)


index = this->_indexFromOffset(point);        改爲=>      index = this->indexOfTouch(point);





(2)CCTableView problem with reloadData()

Create a Tableview, first loaded 0 data
numberOfCellsInTableView (CCTableView * table) {
CCLOG ("WarListLayer");
return arr-> count ();

}
When the network to return data, there are 10 data
I use tableView-> reloadData ();

tableview can not be displayed

When I drag the tableview, the tableview will be able to display.


--------------------------------------------------------------------------------------------------

try this when your data size is changed

CCPoint pos = tableView->getContainer()->getPosition();
tableView->getContainer()->setPosition( ccpAdd(pos,ccp(0,-CellHeight*numberOfAddedData)) );
tableView->reloadData();

-----------------------------------------------------------------------

//我是這麼處理的,好象更好使一些吧!

if(m_pTableView)

{

m_pTableView->reloadData();

CCPoint pt = m_pTableView->minContainerOffset();

m_pTableView->setContentOffset(pt);

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章