cocos2d-x 源码分析 之 CCTableView源码分析(附使用方法讨论)

来源:互联网 发布:淘宝白号怎么升一心 编辑:程序博客网 时间:2024/06/06 00:33
cocos2d-x 源码分析 之 CCTableView源码分析(附使用方法讨论)
分类: cocos2d-x 源码分析 57人阅读 评论(0) 收藏 举报
cocos2d-x游戏源码

目录(?)[+]

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中实现)


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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。
(在文章的最后,我会给出一个例子)。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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。
下面来看第二个函数。
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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赋值。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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方法。)
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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方法。
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }
0 0