现在的位置: 首页 > 综合 > 正文

CCTableView源码分析

2018年04月20日 ⁄ 综合 ⁄ 共 27702字 ⁄ 字号 评论关闭

目录(?)[-]

  1. 继承结构
  2. 相关类的分析
    1. CCTableViewCell
    2. CCTableViewDataSource
    3. CCTableViewDelegate
  3. CCTableView源码分析
    1. 创建时

      1. initWithViewSize
      2. _updateCellPositions
      3. _updateContentSize
    2. 滚动时
      1. _indexFromOffset
      2. _moveCellOutOfSight
      3. updateCellAtIndex
    3. 触摸
      1. ccTouchBegan
      2. ccTouchMoved
      3. ccTouchEnded
    4. 常见的操作
      1. reloadData
      2. refreshData
    5. 小结

cocos2d-x源码总目录

http://blog.csdn.net/u011225840/article/details/31743129

源码来自2.x,转载请注明

1.继承结构

首先来看下CCTableView的继承结构

从继承结构上看,CCTableView是一种CCScrollView,所以为了研究CCTableView的源码,清先去了解CCScrollView的源码http://blog.csdn.net/u011225840/article/details/30033501。
其次,CCTableView也继承了CCScrollViewDelegate,从后面的源码分析中,我们可以看出主要是为了实现scrollViewDidScroll这个函数。从而使用CCScrollView的滚动时,可以实现CCTableView自己本身的操作。(如果你看到这里不懂,请务必先弄懂CCScrollView的源码。)
最后,除了继承结构,我们还需要了解三个重要的类。
CCTableViewCell,CCTableViewDelegate,CCTableViewDataSource。通过这三个类,CCTableView将数据与其他操作解耦。

2.相关类的分析

2.1CCTableViewCell

CCtableViewCell主要是含有一个唯一的标识符,允许TableView通过不同的idx来更新TableviewCell。
一般情况下,会写一个CustomCell来继承该类,该Cell上有每一个cell的样式(含有label?含有sprite?全在该cell中实现)


  1. class CCTableViewCell: public CCNode, public CCSortableObject  
  2. {  
  3. public:  
  4.     CCTableViewCell() {}  
  5.     /** 
  6.      * The index used internally by SWTableView and its subclasses 
  7.      */  
  8.     unsigned int getIdx();  
  9.     void setIdx(unsigned int uIdx);  
  10.     /** 
  11.      * Cleans up any resources linked to this cell and resets <code>idx</code> property. 
  12.      */  
  13.     void reset();  
  14.   
  15.     void setObjectID(unsigned int uIdx);  
  16.     unsigned int getObjectID();  
  17. private:  
  18.     unsigned int m_uIdx;  
  19. };  


2.2CCTableViewDataSource

CCTableViewDataSource是非常重要的一个类,TableView的数据相关的处理都与该类有关,请看他提供的四个函数,注释已经给出哦。
一般情况下,我们会让一个Custom类来继承他并实现方法。该Custom类一般是继承DataSource,TableViewDelegate,与一个CClayer,并含有一个CCTableView。
(在文章的最后,我会给出一个例子)。

  1. //根据不同的idx,来告诉tableview cell的大小
      
  2.     virtual CCSize tableCellSizeForIndex(CCTableView *table, unsigned int idx) {  
  3.         return cellSizeForTable(table);  
  4.     };  
  5.      
  6.     //提供一个通用的方法,给出table的cell大小,如果该table的cell大小都一样,一般都一样。。
      
  7.     virtual CCSize cellSizeForTable(CCTableView *table) {  
  8.         return CCSizeZero;  
  9.     };  
  10.      
  11.     //根据不同的idx,获得table的相应cell,一会分析table的dequeceCell时,再详细讲解此方法。
      
  12.     virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx) = 0;  
  13.      
  14.     //返回table的cell个数。   
  15.     virtual unsigned int numberOfCellsInTableView(CCTableView *table) = 0;  

2.3 CCTableViewDelegate


提供了几个Delegate函数,以供TableView使用。Delegate的用法我在CCScrollView源码分析中已经说过,这里不再赘述。

 virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell) = 0;
这里只说下必须实现的这个函数,当table通过idx获取用户正在触摸该cell后,一定会调用该方法。(选择某个物件后,给人物穿上,就是通过这个方法来响应。)

3.CCTableView源码分析

3.1创建时

CCTableView提供了两个create函数
create(CCTableViewDataSource* dataSource, CCSize size)
create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container)
第一个函数,调用了第二个,将container置为NULL。
下面来看第二个函数。

  1. CCTableView* CCTableView::create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container)  
  2. {  
  3.     CCTableView *table = new CCTableView();  
  4.     table->initWithViewSize(size, container);  
  5.     table->autorelease();  
  6.     table->setDataSource(dataSource);  
  7.     table->_updateCellPositions();  
  8.     table->_updateContentSize();  
  9.   
  10.     return table;  
  11. }  

话说这种风格也不怕堆内存空间不足么。
发现三个重要的函数:

3.1.1 initWithViewSize

  1. bool CCTableView::initWithViewSize(CCSize size, CCNode* container/* = NULL*/)  
  2. {  
  3.     if (CCScrollView::initWithViewSize(size,container))  
  4.     {  
  5.         m_pCellsUsed      = new CCArrayForObjectSorting();  
  6.         m_pCellsFreed     = new CCArrayForObjectSorting();  
  7.         m_pIndices        = new std::set<unsigned int>();  
  8.         m_eVordering      = kCCTableViewFillBottomUp;  
  9.         this->setDirection(kCCScrollViewDirectionVertical);  
  10.   
  11.         CCScrollView::setDelegate(this);  
  12.         return true;  
  13.     }  
  14.     return false;  
  15. }  

cellsUsed是用来存放正在使用的,显示在view上面的cell。
cellsFreed是用来存放暂时不使用的,没在view上面显示的cell(从cellsUsed被移除后添加进cellsFreed),cellsFreed提供了一种缓存机制。允许我们从tableCellAtIndex中拿到cells,不需要重新创建他,只需要根据idx更新下显示。
indices是用来存放每个cell应该占据的位置区域值。

3.1.2 _updateCellPositions

根据CCTableView呈现的方向以及order,给indices赋值。

  1. void CCTableView::_updateCellPositions() {  
  2.   
  3.     //根据dataSource,更新cell的位置。
      
  4.   
  5.   
  6.     int cellsCount = m_pDataSource->numberOfCellsInTableView(this);  
  7.     m_vCellsPositions.resize(cellsCount + 1, 0.0);  
  8.   
  9.   
  10.     if (cellsCount > 0)  
  11.     {  
  12.         float currentPos = 0;  
  13.         CCSize cellSize;  
  14.         for (int i=0; i < cellsCount; i++)  
  15.         {  
  16.             m_vCellsPositions[i] = currentPos;  
  17.             CCLog("The postion is %f",currentPos);  
  18.             //根据idx获取到相应cell的size   
  19.             cellSize = m_pDataSource->tableCellSizeForIndex(this, i);  
  20.   
  21.             switch (this->getDirection())  
  22.             {  
  23.                 case kCCScrollViewDirectionHorizontal:  
  24.                     currentPos += cellSize.width;  
  25.                     break;  
  26.                 default:  
  27.                     currentPos += cellSize.height;  
  28.                     break;  
  29.             }  
  30.         }  
  31.         //n个cell需要n+1个Pos 来指定位置
      
  32.         m_vCellsPositions[cellsCount] = currentPos;//1 extra value allows us to get right/bottom of the last cell
      
  33.         CCLog("The postion is %f",currentPos);  
  34.     }  
  35.   
  36. }  

3.1.3 _updateContentSize

这个方法调整了CCTableView的大小与偏移。(注意,调整偏移的时候,会调用scrollViewDidScroll方法。)

  1. void CCTableView::_updateContentSize()  
  2. {  
  3.     CCSize size = CCSizeZero;  
  4.     unsigned int cellsCount = m_pDataSource->numberOfCellsInTableView(this);  
  5.   
  6.     //获取到最大的长与宽   
  7.     if (cellsCount > 0)  
  8.     {  
  9.         float maxPosition = m_vCellsPositions[cellsCount];  
  10.   
  11.         switch (this->getDirection())  
  12.         {  
  13.             case kCCScrollViewDirectionHorizontal:  
  14.                 size = CCSizeMake(maxPosition, m_tViewSize.height);  
  15.                 break;  
  16.             default:  
  17.                 size = CCSizeMake(m_tViewSize.width, maxPosition);  
  18.                 break;  
  19.         }  
  20.     }  
  21.   
  22.     //获取后调用CCScrollView的setContenSize
      
  23.     this->setContentSize(size);  
  24.   
  25.     //调整方向与初始偏移offset   
  26.     if (m_eOldDirection != m_eDirection)  
  27.     {  
  28.         if (m_eDirection == kCCScrollViewDirectionHorizontal)  
  29.         {  
  30.             this->setContentOffset(ccp(0,0));  
  31.         }  
  32.         else  
  33.         {  
  34.             //这里其实不是很懂   
  35.             this->setContentOffset(ccp(0,this->minContainerOffset().y));  
  36.         }  
  37.         m_eOldDirection = m_eDirection;  
  38.     }  
  39.   
  40. }  

注释所示,我有个小问题,如果是垂直方向的,则会把初始位置放到minContainerOffset上,为何这样,没懂。。。
下面一节重点讲解scrollviewDidScroll

3.2 滚动时

根据父类CCScrollView,每次设置偏移后,会调用scrollviewDidScroll方法。

  1. void CCTableView::scrollViewDidScroll(CCScrollView* view)  
  2. {  
  3.     //继承自CCScrollViewDelegate,并且根据CCScrollView的源码,每次移动时(setContentOffset函数),都会调用这个函数
      
  4.   
  5.     //没有任何元素   
  6.     unsigned int uCountOfItems = m_pDataSource->numberOfCellsInTableView(this);  
  7.     if (0 == uCountOfItems)  
  8.     {  
  9.         return;  
  10.     }  
  11.   
  12.     //tableviewdelegate的DidScroll调用
      
  13.     if(m_pTableViewDelegate != NULL) {  
  14.         m_pTableViewDelegate->scrollViewDidScroll(this);  
  15.     }  
  16.   
  17.       
  18.     unsigned int startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;  
  19.   
  20.     //需要乘以-1的原因很简单,当offset处于正数时,即为cell在初始位置还要往右拉,此时得到的startIdx肯定是不存在的。
      
  21.     CCPoint offset = ccpMult(this->getContentOffset(), -1);  
  22.     maxIdx = MAX(uCountOfItems-1, 0);  
  23.   
  24.     if (m_eVordering == kCCTableViewFillTopDown)  
  25.     {  
  26.         offset.y = offset.y + m_tViewSize.height/this->getContainer()->getScaleY();  
  27.     }  
  28.     //查到起始的startIdx   
  29.     startIdx = this->_indexFromOffset(offset);  
  30.     //CCLog("The offset is %f",offset.x);
      
  31.     //CCLog("The start index is %d",startIdx);
      
  32.       
  33.     if (startIdx == CC_INVALID_INDEX)  
  34.     {  
  35.         startIdx = uCountOfItems - 1;  
  36.     }  
  37.   
  38.     if (m_eVordering == kCCTableViewFillTopDown)  
  39.     {  
  40.         offset.y -= m_tViewSize.height/this->getContainer()->getScaleY();  
  41.     }  
  42.     else  
  43.     {  
  44.         offset.y += m_tViewSize.height/this->getContainer()->getScaleY();  
  45.     }  
  46.     //起始offset加上显示View的宽度就是endIdx的offset
      
  47.     offset.x += m_tViewSize.width/this->getContainer()->getScaleX();  
  48.   
  49.     endIdx   = this->_indexFromOffset(offset);  
  50.   
  51.     //如果endIdx 超过,则将endIdx置为最大值
      
  52.     if (endIdx == CC_INVALID_INDEX)  
  53.     {  
  54.         endIdx = uCountOfItems - 1;  
  55.     }  
  56.       
  57.     if(startIdx > endIdx)  
  58.     {  
  59.         int tmp = startIdx;  
  60.         startIdx = endIdx;  
  61.         endIdx = tmp;  
  62.     }  
  63.   
  64.   
  65.     if (m_pCellsUsed->count() > 0)  
  66.   
  67.     {  
  68.         CCTableViewCell* cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(0);  
  69.         //找出正在使用的cell,只要是idx小于startIdx的,就移出。
      
  70.         idx = cell->getIdx();  
  71.         while(idx <startIdx)  
  72.         {  
  73.             this->_moveCellOutOfSight(cell);  
  74.             if (m_pCellsUsed->count() > 0)  
  75.             {  
  76.                 cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(0);  
  77.                 idx = cell->getIdx();  
  78.             }  
  79.             else  
  80.             {  
  81.                 break;  
  82.             }  
  83.         }  
  84.     }  
  85.     if (m_pCellsUsed->count() > 0)  
  86.     {  
  87.         CCTableViewCell *cell = (CCTableViewCell*)m_pCellsUsed->lastObject();  
  88.         idx = cell->getIdx();  
  89.         //同上,移除所有大于endIdx的cell
      
  90.         while(idx <= maxIdx && idx > endIdx)  
  91.         {  
  92.             this->_moveCellOutOfSight(cell);  
  93.             if (m_pCellsUsed->count() > 0)  
  94.             {  
  95.                 cell = (CCTableViewCell*)m_pCellsUsed->lastObject();  
  96.                 idx = cell->getIdx();  
  97.   
  98.             }  
  99.             else  
  100.             {  
  101.                 break;  
  102.             }  
  103.         }  
  104.     }  
  105.   
  106.     //更新在start和end之间的cell   
  107.     for (unsigned int i=startIdx; i <= endIdx; i++)  
  108.     {  
  109.         //if ([m_pIndices containsIndex:i]),indices存在即表明该位置上的cell已经被update。
      
  110.         if (m_pIndices->find(i) != m_pIndices->end())  
  111.         {  
  112.             continue;  
  113.         }  
  114.         this->updateCellAtIndex(i);  
  115.     }  
  116. }  

该函数中,调用了三个内部函数:

3.2.1 _indexFromOffset

  1. unsigned int CCTableView::_indexFromOffset(CCPoint offset)  
  2. {  
  3.     int index = 0;  
  4.     const int maxIdx = m_pDataSource->numberOfCellsInTableView(this)-1;  
  5.   
  6.     //如果是垂直方向上,并且是TopDown的,则改变offset.y
      
  7.     if (m_eVordering == kCCTableViewFillTopDown)  
  8.     {  
  9.         offset.y = this->getContainer()->getContentSize().height - offset.y;  
  10.     }  
  11.     //获取该点处于哪个index中   
  12.     index = this->__indexFromOffset(offset);  
  13.     if (index != -1)  
  14.     {  
  15.         index = MAX(0, index);  
  16.         if (index > maxIdx)  
  17.         {  
  18.             index = CC_INVALID_INDEX;  
  19.         }  
  20.     }  
  21.   
  22.     return index;  
  23. }  
  24.   
  25. int CCTableView::__indexFromOffset(CCPoint offset)  
  26. {  
  27.     int low = 0;  
  28.     int high = m_pDataSource->numberOfCellsInTableView(this) - 1;  
  29.     float search;  
  30.     //根据方向来判断需要寻找的是x还是y坐标   
  31.     switch (this->getDirection())  
  32.     {  
  33.         case kCCScrollViewDirectionHorizontal:  
  34.             search = offset.x;  
  35.             break;  
  36.         default:  
  37.             search = offset.y;  
  38.             break;  
  39.     }  
  40.     //二分查找,找出点在哪个cell的区间内,返回index   
  41.     while (high >= low)  
  42.     {  
  43.         int index = low + (high - low) / 2;  
  44.         float cellStart = m_vCellsPositions[index];  
  45.         float cellEnd = m_vCellsPositions[index + 1];  
  46.         CCLog("The start cell is %f",cellStart);  
  47.         if (search >= cellStart && search <= cellEnd)  
  48.         {  
  49.             return index;  
  50.         }  
  51.         else if (search < cellStart)  
  52.         {  
  53.             high = index - 1;  
  54.         }  
  55.         else  
  56.         {  
  57.             low = index + 1;  
  58.         }  
  59.     }  
  60.   
  61.     if (low <= 0) {  
  62.         return 0;  
  63.     }  
  64. <span style="white-space:pre">    </span>//结果是-1则表示超出最大距离,在外部将被赋值为最大距离
      
  65.     return -1;  
  66. }  

3.2.2 _moveCellOutOfSight

  1. void CCTableView::_moveCellOutOfSight(CCTableViewCell *cell)  
  2. {  
  3.     //此时调用delegate方法。cellWillCycle
      
  4.     if(m_pTableViewDelegate != NULL) {  
  5.         m_pTableViewDelegate->tableCellWillRecycle(this, cell);  
  6.     }  
  7.   
  8.     //做数据处理   
  9.     m_pCellsFreed->addObject(cell);  
  10.     m_pCellsUsed->removeSortedObject(cell);  
  11.     m_pIndices->erase(cell->getIdx());  
  12.     // [m_pIndices removeIndex:cell.idx];
      
  13.     cell->reset();  
  14.     if (cell->getParent() == this->getContainer()) {  
  15.         this->getContainer()->removeChild(cell, true);;  
  16.     }  
  17. }  

3.2.3 updateCellAtIndex

  1. void CCTableView::updateCellAtIndex(unsigned int idx)  
  2. {  
  3.     if (idx == CC_INVALID_INDEX)  
  4.     {  
  5.         return;  
  6.     }  
  7.   
  8.     unsigned int uCountOfItems = m_pDataSource->numberOfCellsInTableView(this);  
  9.     if (0 == uCountOfItems || idx > uCountOfItems-1)  
  10.     {  
  11.         return;  
  12.     }  
  13.     //首先将cell移除used   
  14.     CCTableViewCell* cell = this->cellAtIndex(idx);  
  15.     if (cell)  
  16.     {  
  17.         this->_moveCellOutOfSight(cell);  
  18.     }  
  19.     //调用该方法,根据idx获取新cell
      
  20.     cell = m_pDataSource->tableCellAtIndex(this, idx);  
  21.     //设置cell   
  22.     this->_setIndexForCell(idx, cell);  
  23.     //将cell加到used中   
  24.     this->_addCellIfNecessary(cell);  
  25. }  

可以看出,update时,先将所有cell移出,再通过dataSource的tableCellAtIndex来获取更新cell。如果不设置缓存freed,会造成性能瓶颈。
  1. void CCTableView::_setIndexForCell(unsigned int index, CCTableViewCell *cell)  
  2. {  
  3.     //设置cell的锚点,位置与idx
      
  4.     cell->setAnchorPoint(ccp(0.0f, 0.0f));  
  5.     cell->setPosition(this->_offsetFromIndex(index));  
  6.     CCLog("The cell position is %f",this->_offsetFromIndex(index).x);  
  7.     cell->setIdx(index);  
  8. }  
  1. void CCTableView::_addCellIfNecessary(CCTableViewCell * cell)  
  2. {  
  3.     if (cell->getParent() != this->getContainer())  
  4.     {  
  5.         this->getContainer()->addChild(cell);  
  6.     }  
  7.     m_pCellsUsed->insertSortedObject(cell);  
  8.     m_pIndices->insert(cell->getIdx());  
  9.     // [m_pIndices addIndex:cell.idx];
      
  10. }  

看到这里,基本上CCTableView的重点已经看完了。下面继续。

3.3 触摸

3.3.1 ccTouchBegan

  1. bool CCTableView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     if (!this->isVisible()) {  
  4.         return false;  
  5.     }  
  6.     //父类的ccTouchBegan调用,可以获取touches的多少,并且判断出行为
      
  7.     bool touchResult = CCScrollView::ccTouchBegan(pTouch, pEvent);  
  8.   
  9.     //啊哦,tableview不支持缩放了哦,只支持滚动
      
  10.     if(m_pTouches->count() == 1) {  
  11.         unsigned int        index;  
  12.         CCPoint           point;  
  13.   
  14.         //获取touch在该TableView坐标系下的CCpoint
      
  15.         point = this->getContainer()->convertTouchToNodeSpace(pTouch);  
  16.         //获取该point在数据中的位置   
  17.         index = this->_indexFromOffset(point);  
  18.         //获取在该index上的tableviewCell
      
  19.         if (index == CC_INVALID_INDEX)  
  20.         {  
  21.             m_pTouchedCell = NULL;  
  22.         }  
  23.         else  
  24.         {  
  25.             m_pTouchedCell  = this->cellAtIndex(index);  
  26.         }  
  27.         //如果该cell存在并且delegate存在,调用delegate的方法,比如说可以pressed
      
  28.         if (m_pTouchedCell && m_pTableViewDelegate != NULL) {  
  29.             m_pTableViewDelegate->tableCellHighlight(this, m_pTouchedCell);  
  30.         }  
  31.     }  
  32.     //当触摸点个数不为1,但是存在正在触摸的cell时,将该cell置空,并且调用取消高亮的方法比如说unpressed
      
  33.     else if(m_pTouchedCell) {  
  34.         if(m_pTableViewDelegate != NULL) {  
  35.             m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);  
  36.         }  
  37.   
  38.         m_pTouchedCell = NULL;  
  39.     }  
  40.   
  41.     return touchResult;  
  42. }  


3.3.2 ccTouchMoved

  1. void CCTableView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     //先调用父类的move   
  4.     CCScrollView::ccTouchMoved(pTouch, pEvent);  
  5.     //如果移动过程中还存在触摸的cell,则置空并调用delegate
      
  6.     if (m_pTouchedCell && isTouchMoved()) {  
  7.         if(m_pTableViewDelegate != NULL) {  
  8.             m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);  
  9.         }  
  10.   
  11.         m_pTouchedCell = NULL;  
  12.     }  
  13. }  

3.3.3 ccTouchEnded

  1. void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     if (!this->isVisible()) {  
  4.         return;  
  5.     }  
  6.     //move距离过短,则识别为触摸到cell的动作并获取到该cell,调用Delegate的方法。
      
  7.     if (m_pTouchedCell){  
  8.         CCRect bb = this->boundingBox();  
  9.         bb.origin = m_pParent->convertToWorldSpace(bb.origin);  
  10.   
  11.         if (bb.containsPoint(pTouch->getLocation()) && m_pTableViewDelegate != NULL)  
  12.         {  
  13.             m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);  
  14.             m_pTableViewDelegate->tableCellTouched(this, m_pTouchedCell);  
  15.         }  
  16.   
  17.         m_pTouchedCell = NULL;  
  18.     }  
  19.   
  20.     CCScrollView::ccTouchEnded(pTouch, pEvent);  
  21. }  

3.4 常见的操作

3.4.1 reloadData

  1. void CCTableView::reloadData()  
  2. {  
  3.     m_eOldDirection = kCCScrollViewDirectionNone;  
  4.     CCObject* pObj = NULL;  
  5.     CCARRAY_FOREACH(m_pCellsUsed, pObj)  
  6.     {  
  7.         CCTableViewCell* cell = (CCTableViewCell*)pObj;  
  8.   
  9.         if(m_pTableViewDelegate != NULL) {  
  10.             m_pTableViewDelegate->tableCellWillRecycle(this, cell);  
  11.         }  
  12.   
  13.         m_pCellsFreed->addObject(cell);  
  14.         cell->reset();  
  15.         if (cell->getParent() == this->getContainer())  
  16.         {  
  17.             this->getContainer()->removeChild(cell, true);  
  18.         }  
  19.     }  
  20.   
  21.     m_pIndices->clear();  
  22.     m_pCellsUsed->release();  
  23.     m_pCellsUsed = new CCArrayForObjectSorting();  
  24.   
  25.     this->_updateCellPositions();  
  26.     this->_updateContentSize();  
  27.     if (m_pDataSource->numberOfCellsInTableView(this) > 0)  
  28.     {  
  29.         this->scrollViewDidScroll(this);  
  30.     }  
  31. }  

当你的数据源发生改变后,请调用reloadData。从中可以看出,他重新计算了与数据相关的操作。

3.4.2 refreshData

  1. void CCTableView::refreshData()  
  2. {  
  3.     int startIndex = 0;  
  4.     int endIndex = 0;  
  5.     // 取出当前可见的item的收尾索引
      
  6.     getStartEndIndex(startIndex, endIndex);  
  7.   
  8.     // 只刷新看见的item   
  9.     //unsigned int uCountOfItems = m_pDataSource->numberOfCellsInTableView(this);
      
  10.     for(unsigned int i = startIndex; i <= endIndex; ++i)  
  11.     {  
  12.         this->updateCellAtIndex(i);  
  13.     }  
  14. }  

区别是显而易见的,数据源没有发生改变,只是startIndex和endIndex发生了改变,并更新显示。

4.小结

   1. CCTableView三基友:
CCTableViewCell,负责单个cell,含有唯一idx用于区别。
CCTableViewDataSource,负责数据源相关,包括数据个数,数据根据不同idx的获取,数据size等。
CCTableViewDelegate,负责delegate操作。
  2.CCTableView不但继承了CCScrollView,同时也继承了CCScrollViewDelegate。

抱歉!评论已关闭.