接上回 CCScrollView 继续,在GUI 里还有个 CCScrollView 的子类---CCTableView 。 这个名字应该是从 IOS 里的 UITableView来的,其实是跟WP8的 Listbox 效果一样,实现

大数据的虚拟化展示, 不管在应用还是游戏里都是很常见的控件。 比如下面的 《天天爱消除》 的分数展示

下面我们用 CCTableView 一步步实现上面的效果,

一、创建承载它的容器

我们选用一个Layer,

class ListViewLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate

{

public:

    virtual bool init();  

    

    virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);

    virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view);

    //处理触摸事件

    virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);

    //每一项的宽度和高度

    virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);

    //生成列表每一项的内容

    virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);

    //一共多少项

    virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);

    CREATE_FUNC(ListViewLayer);

};

该 Layer 实现了 CCTableViewDataSource 和 CCTableViewDelegate 这两个接口,

分别为 CCTableView 提供数据源 和 响应事件,后面会作为 CCTableView 的 Delegate 存在

二、 创建并添加 CCTableView 对象

bool ListViewLayer::init()
{
    bool bRet = false;
    do
    {
        CC_BREAK_IF( !CCLayer::init() );         bg = CCTextureCache::sharedTextureCache()->addImage("bg.png");
        bg2 = CCTextureCache::sharedTextureCache()->addImage("bg2.png");         CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();         CCTableView* pTableView = CCTableView::create(this, CCSizeMake(visibleSize.width, visibleSize.height));
        pTableView->setDirection(kCCScrollViewDirectionVertical);
        pTableView->setPosition(CCPointZero);
        pTableView->setDelegate(this);  //将Delegate对象设置为刚才创建的容器.
        pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);         this->addChild(pTableView);
        pTableView->reloadData();         bRet = true;
    }
    while();     return bRet;
}

三、 为CCTableView提供数据

首先要 通过 numberOfCellsInTableView 指定 TableView 的单元个数,然后在 cellSizeForTable 里指定具体的单元格的尺寸大小,最后在 tableCellAtIndex 里控制每个单元格的具体数据。

代码如下:

unsigned int ListViewLayer::numberOfCellsInTableView(CCTableView *table)

{

    return ;

}

CCSize ListViewLayer::cellSizeForTable(CCTableView *table)

{

    return CCSizeMake(CCDirector::sharedDirector()->getVisibleSize().width, );

}

CCTableViewCell* ListViewLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)

{

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

    CCString *pString = CCString::createWithFormat("%d", idx + );

    //这里要注意,因为是单元格是会重用的,所以不一定每次都要新建。

    CCTableViewCell *pCell = table->dequeueCell();

    if (!pCell) 

    {

        pCell = new CCTableViewCell();

        pCell->autorelease();

        //Add background.

        CCSprite *pSprite;

        if(UserIndex == idx)

        {

            pSprite = CCSprite::createWithTexture(bg2);

        }

        else

        {

            pSprite = CCSprite::createWithTexture(bg);

        }

        pSprite->setAnchorPoint(CCPointZero);

        pSprite->setPosition(CCPointZero);

        pSprite->setTag();

        pCell->addChild(pSprite);

        //Add Icon.

        pSprite = CCSprite::create("Icon.png");

        pSprite->setPosition(ccp(, ));

        pCell->addChild(pSprite);

        //Add Rate.

        CCLabelTTF *pLabel = CCLabelTTF::create(pString->getCString(), "Arial", 70.0);

        pLabel->setPosition(ccp(, ));

        pLabel->setTag();

        pCell->addChild(pLabel);

        //Add Name.

        pLabel = CCLabelTTF::create("Ghost Person", "Arial", 40.0);

        pLabel->setPosition(ccp(, ));

        pLabel->setAnchorPoint(CCPointZero);

        pLabel->setTag();

        pCell->addChild(pLabel);

    }

    else

    {

        CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag();

        pLabel->setString(pString->getCString());

        if(UserIndex == idx)//根据ID创建不同的效果.

        {

            CCSprite* bg3 = (CCSprite*)pCell->getChildByTag();

            bg3->setTexture(bg2);

        }

        else

        {

            CCSprite* bg3 = (CCSprite*)pCell->getChildByTag();

            bg3->setTexture(bg);

        }

    }

    return pCell; }

就这三步,大功告成了,附上效果图:

丑是丑了一点啊,不过已经基本成型了。

而且发现在920上滑动的时候只有30帧左右,不知道其他手机如何,后面具体再测。

欢迎有兴趣的童鞋加入Cocos2d-x 开发群  qq: 264152376

【Cocos2d-x for WP8 学习整理】(4)CCTableView 实现《天天爱消除》中的得分榜的更多相关文章

  1. 【Cocos2d-x for WP8 学习整理】(5)文字显示全整理

    学习 Cocos2d-x 有一阵了,现在要做个小东西,第一步就遇到了文字展示的问题,于是想把可能遇到的问题统统整理一下.这一部分也不局限于wp8,全平台应该都是一个解决方案. 先在脑袋里大致想了一下, ...

  2. 【Cocos2d-x for WP8 学习整理】(2)Cocos2d-Html5 游戏 《Fruit Attack》 WP8移植版 开源

    这一阵花了些时间,把 cocos2d-html5 里的sample 游戏<Fruit Attack>给移植到了WP8上来,目前已经实现了基本的功能,但是还有几个已知的bug,比如WP8只支 ...

  3. 【Cocos2d-x for WP8 学习整理】(1)创建一个新项目

    喜大普奔                         10.1假期之前看到了一个很振奋的消息,就是随着Cocos2d-x 2.2的发布,WP8/WIN8有史以来第一次的合并到主版本了. 之前 V2 ...

  4. 【Cocos2d-x for WP8 学习整理】(3)CCScrollView 实现捕鱼达人一样的场景选择界面

    UI 界面一般是游戏里比较独立的地方,因为游戏引擎一般都比较注意基础的功能封装,很少会关注UI,但是 UI 确是玩家第一眼看到的效果,因此能否实现一个美观的UI对于提升游戏的整体美观有着很大的帮助. ...

  5. [Cocos2d-x for WP8学习笔记] HelloWorld结构分析

    先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...

  6. js数组学习整理

    原文地址:js数组学习整理 常用的js数组操作方法及原理 1.声明数组的方式 var colors = new Array();//空的数组 var colors = new Array(3); // ...

  7. TweenMax学习整理--特有属性

    TweenMax学习整理--特有属性   构造函数:TweenMax(target:Object, duration:Number, vars:Object) target:Object -- 需要缓 ...

  8. HttpClient学习整理

    HttpClient简介HttpClient 功能介绍    1. 读取网页(HTTP/HTTPS)内容    2.使用POST方式提交数据(httpClient3)    3. 处理页面重定向    ...

  9. [Cocos2d-x for WP8学习笔记] HelloWorld

    Cocos2d-x 是一个支持多平台的 2D 手机游戏引擎,使用 C++ 开发,基于OpenGL ES,基于Cocos2d-iphone,支持 WOPhone, iOS 4.1, Android 2. ...

随机推荐

  1. 微信JS接口

      微信JS接口 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置来源:http://www.cnblogs.com/txw1958/p/ ...

  2. COGS396. [网络流24题]魔术球问题(简化版

    问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为 1,2,3,4......的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全平 ...

  3. 向shell脚本中传入参数

    写一个 程序名为    test.sh    可带参数为 start 和 stop 执行  test.sh start执行  start 内容的代码 执行 test.sh stop 执行 stop 内 ...

  4. wpf 客户端 添加qq客服咨询

    使用qq推广 站点:http://shang.qq.com/v3/widget.html 复制里面的html代码: <a target=" src="http://wpa.q ...

  5. jQuery下拉菜单插件Tendina.

    插件效果: 下载地址和文档: https://github.com/iprignano/tendina

  6. PC工作原理

    提到"技术"这个词时,大多数人都会想到计算机.事实上,我们生活中的方方面面都离不开计算机部件.家里的电器设备有内置的微处理器,例如电视机.甚至汽车里也装有计算机.但是,提到计算机大 ...

  7. 盒子模型简单理解(box-sizing)

    普通解析: 概念图示和公式: html结构 <div class="num1"></div> 1.只写 width.height(写背景是为了区分) .nu ...

  8. 关于Visual Studio 2015中没有报表项(ReportViewer)的解决方案。

    没有报表,一般默认安装之后会出现这种情况,在安装的时候选择自定义安装,把Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools勾选上,安装之后就 ...

  9. [nosql之redis]yum安装redis

    1.首先对于这种nosql来说目前我用到的功能很少,所以感觉没有必要去优化他跟不需要去编译安装.今天来介绍下一个yum安装redis 步骤1:安装扩展yum库 [root@localhost ~]# ...

  10. 2015.4.19 为什么footer下a的索引值那么大

    1.问题demo:为什么footer下a的索引值那么大,index不是查找兄弟级别的元素么?而且还限定了范围在footer下的a的情况下. 解决方法:alert( $("#footer a& ...