COCOS2DX - TableView的使用

COCOS2DX - TableView的使用,第1张

概述今天想学习一下 cocos2dx的listview的使用,结果百度等搜索时发现结果非常少。。正纳闷呢,才发现在2.1.3之后ListView就被TableView代替了。 由于之前已经有了Android一年左右的经验,有时候会先入为主地认为那种效果就应该是ListView吧,包括ScorllView和PageView,我估计之后学习的时候也会有些不同吧。 好了废话不多说,一起来看一下cocos2d

今天想学习一下 cocos2dx的ListvIEw的使用,结果百度等搜索时发现结果非常少。。正纳闷呢,才发现在2.1.3之后ListVIEw就被tableVIEw代替了。
由于之前已经有了AndroID一年左右的经验,有时候会先入为主地认为那种效果就应该是ListVIEw吧,包括ScorllVIEw和PageVIEw,我估计之后学习的时候也会有些不同吧。
好了废话不多说,一起来看一下cocos2dx里面的CCtableVIEw吧。

可以看到CCtableVIEw继承自CCScrollVIEw,于是我猜想在使用这个控件的时候需要实现一些关于ScrollVIEw的方法比如触摸、滑动等。
初始化一个CCtableVIEw:

//初始化的时候第一个参数是CCtableVIEwDataSource,第二个参数代表tablevIEw的大小    CCtableVIEw * table = CCtableVIEw::create(this,CCSize(size.wIDth/2,size.height/2));    //设置delegate代理    table->setDelegate(this);    //设置tablevIEw的滑动的方向    //kCCScrollVIEwDirectionHorizontal 水平    //kCCScrollVIEwDirectionVertical 竖直    table->setDirection(kCCScrollVIEwDirectionVertical);    //CCtableVIEw默认是以左下角点设置坐标位置的,它继承自cclayer,这一点不难理解    //table->setAnchorPoint(ccp(0,0));    table->setposition(ccp(0,size.height/2));    this->addChild(table);    cclOG("垂直List建立");

去掉注释和输出的话可以看到只需要5行代码。
但是我们还需要对一些方法进行实现。

//继承自以上的接口需要实现的方法如下    //这个函数是从CCtableVIEwDelegate继承下来的,其他三个是从CCtableVIEwDataSource继承下来的    voID tableCelltouched(CCtableVIEw * table,CCtableVIEwCell * cell);    CCSize tableCellSizeforIndex(CCtableVIEw * table,unsigned int index);    CCtableVIEwCell * tableCellAtIndex(CCtableVIEw * table,unsigned int index);    unsigned int numberOfCellsIntableVIEw(CCtableVIEw * table);    //以下俩个函数可以覆写,也可以不覆写,是从CCtableVIEwDelegate继承下来的    voID tableCellHighlight(CCtableVIEw * table,CCtableVIEwCell * cell);    voID tableCellUnhighlight(CCtableVIEw * table,CCtableVIEwCell * cell);    //因为CCtableVIEw继承自CCScrollVIEw所以要实现这俩个方法,里边一般都不写东西    voID scrollVIEwDIDScroll(CCScrollVIEw *) {};    voID scrollVIEwDIDZoom(CCScrollVIEw *) {};

具体实现:

//这里设置cell被点击以后的回调函数voID ListVIEwDemo::tableCelltouched(CCtableVIEw * table,CCtableVIEwCell * cell){    cclog("%d:touched!",cell->getIDx()+1);}//这个函数是用来获得cell的CCtableVIEwCell * ListVIEwDemo::tableCellAtIndex(CCtableVIEw * table,unsigned int index){    CCString * string = CCString::createWithFormat("%d",index+1);    //获得一个可用的cell,因为在我们滑动cell的时候有些cell是显示的,有些不是显示出来的,把没有渲染的cell拿过来    //这样就不用重新new出一个cell了,这样的话可以减小内存的开销    CCtableVIEwCell * cell = table->dequeueCell();    if(!cell)    {        cell = new CCtableVIEwCell();        cell->autorelease();        //添加背景图片到cell中,便于区分边界        CCSprite * background = CCSprite::create("cell.png");        background->setAnchorPoint(ccp(0,0));        background->setposition(CCPointZero);        cell->addChild(background,0);        //添加文本信息到cell中        cclabelTTF * text = cclabelTTF::create(string->getCString(),"",20);        text->setposition(ccp(25,25));        text->setTag(1);        text->setcolor(ccc3(255,0,0));        cell->addChild(text,1);        //添加精灵到cell的中心位置        CCSprite * sprite = CCSprite::create("icon.png");        sprite->setposition(ccp(50,60));        cell->addChild(sprite,1);    }    //else中获得是没有渲染出来的cell,cell中原有的内容还存在    else    {        //改变原来cell中的文本信息        cclabelTTF * text = (cclabelTTF *)cell->getChildByTag(1);        text->setString(string->getCString());    }    return cell;}//这里设置每个cell的大小CCSize ListVIEwDemo::tableCellSizeforIndex(CCtableVIEw * table,unsigned int index){    return CCSize(100,50);}//这里设置一共有多少个cellunsigned int ListVIEwDemo::numberOfCellsIntableVIEw(CCtableVIEw * table){    return 20;}//如果某个cell被点击了,则会调用此函数voID ListVIEwDemo::tableCellHighlight(CCtableVIEw * table,CCtableVIEwCell * cell){    cclog("highlight!");}//点击之后会调用这个函数,观察这几个函数的调用顺序,发现highlight首先调用//unhighlight然后调用,最后是tableCelltouchedvoID ListVIEwDemo::tableCellUnhighlight(CCtableVIEw * table,CCtableVIEwCell * cell){    cclog("unhighlight!");}

经测试,关于点击cell的调用顺序如下:

总结

以上是内存溢出为你收集整理的COCOS2DX - TableView的使用全部内容,希望文章能够帮你解决COCOS2DX - TableView的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064584.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存