關於cocos2dx中tableView的一些理解

2021-06-26 14:26:37 字數 4852 閱讀 1002

先看**:

.h檔案中:

#ifndef __helloworld_scene_h__

#define __helloworld_scene_h__

#include "cocos2d.h"

#include "cocos-ext.h"

using_ns_cc;

using_ns_cc_ext;

class

helloworld :

public

cclayer,

public

cctableviewdatasource,

public

cctableviewdelegate

;virtual

void scrollviewdidzoom(ccscrollview* view){};

virtual

void tablecelltouched(cctableview* table,cctableviewcell* cell);

virtual

ccsize tablecellsizeforindex(cctableview *table,unsigned

int idx);

virtual

cctableviewcell* tablecellatindex(cctableview *table,unsigned

int idx);

virtual

unsigned

int numberofcellsintableview(cctableview *table);

virtual

void tablecellhighlight(cctableview* table,cctableviewcell* cell){};

virtual

void tablecellunhighlight(cctableview* table,cctableviewcell* cell){};

virtual

void tablecellwillrecycle(cctableview* table,cctableviewcell* cell){};

};#endif

.cpp檔案中

#include "helloworldscene.h"

#include "******audioengine.h"

usingnamespace

cocos2d;

usingnamespace

cocosdenshion;

ccscene* helloworld::scene()

bool

helloworld::init()

ccmenuitemimage*pcloseitem =ccmenuitemimage::

create(

"closenormal.png",

"closeselected.png",

this,

menu_selector(helloworld::menuclosecallback) );

pcloseitem->setposition( ccp(ccdirector::shareddirector()->getwinsize().width -20, 20) );

ccmenu* pmenu = ccmenu::create(pcloseitem,null);

pmenu->setposition( ccpointzero );

this->addchild(pmenu,1);

ccsize

size =

ccdirector

::shareddirector()->getwinsize();

cctableview* tableview = cctableview::create(this, ccsizemake(300, 80));

tableview->setdirection(kccscrollviewdirectionhorizontal);

tableview->setposition(ccp(0,size.height /2));

tableview->setdelegate(this);

this->addchild(tableview,100);

tableview->reloaddata();

returntrue; }

void

helloworld::menuclosecallback(ccobject* psender)

void

helloworld::tablecelltouched(cctableview* table,cctableviewcell* cell)

ccsize

helloworld::tablecellsizeforindex(cctableview* table,unsigned

int idx)

cctableviewcell* helloworld::tablecellatindex(cctableview* table,unsigned

int idx)else

return cell;

}unsigned

inthelloworld::numberofcellsintableview(cctableview* table)

現在執行程式,可以看到的log為:

cocos2d: zhanghaibinlog:29

cocos2d: zhanghaibinlog:new:29

cocos2d: zhanghaibinlog:0

cocos2d: zhanghaibinlog:1

cocos2d: zhanghaibinlog:new:1

cocos2d: zhanghaibinlog:2

cocos2d: zhanghaibinlog:new:2

介面上可以看到3個item

從log可以看出:

程式一開始呼叫了4次tablecellatindex,但是只new了3次。

cctableviewcell* cell = table->dequeuecell();

這句的作用是返回table佇列中的某一項

當idx為29時,佇列中還沒有內容,所以新建乙個。

當idx為0時,佇列中有了乙個29,所以0復用29

當idx為1、2時,都是新建立的。

當向左滑動乙個item時

cocos2d: zhanghaibinlog:3

cocos2d: zhanghaibinlog:new:3

說明idx為3時又建立了乙個新的cell

當向左再滑動乙個item時,可以看到29又出來了

cocos2d: zhanghaibinlog:4

說明idx為4時使用的是29

再向左滑動乙個,看到1出來了

cocos2d: zhanghaibinlog:5

說明idx為5時使用的是1

依此滑動,可以驗證6用的是2、7用的是3、8用的29、9用的1、10用的是2、11用的3、12用的29、……、29用的1

綜上所述,new出來的一共是4個,分別是index為29、1、2、3。所有cell都是直接或是復用這4個cell

當顯示最後三個cell時

繼續向左滑動,滑動隱藏乙個cell然後鬆手時

zhanghaibinlog:26

隱藏兩個時鬆手時

zhanghaibinlog:27

zhanghaibinlog:26

隱藏三個鬆手時:

zhanghaibinlog:28

zhanghaibinlog:27

zhanghaibinlog:26

說明也呼叫了tablecellatindex

所以說如果希望在重用的cell中依照idx從新設定新的內容,需要把else中的語句:

//        cclabelttf* label = (cclabelttf*)cell->getchildbytag(123);

//        label->setstring(string->getcstring());

解注釋

cocos2dx筆記 關於action

1.基本動作instantaction 放置 place 隱藏 hide 顯示 show 可見切換 togglevisibility 2.延時動作 移動到 ccmoveto 移動 ccmoveby 跳躍到 ccjumpto 跳躍 ccjumpby 貝塞爾 ccbezierby 放大到 ccscale...

關於cocos2dx中動畫的整理

1.瞬時動作 ccactioninstant ccplace 用於定位 cchide ccshow 隱藏和顯示 cctogglevisibility 交替變化換是否為隱藏狀態 ccflipx ccflipy分別為水平翻轉和垂直翻轉 瞬時動畫 auto place place create ccp 5...

cocos2dx中關於場景的管理

runwithscene ccscene scene 啟動遊戲,並執行scene 場景。這個方法在主程式啟動時第一次啟動主場景時呼叫。replacescene ccscene scene 直接使用傳入的scene 替換當前場景來切換畫面,當前場景將被釋放。這是切換場景時 最常用的方法。pushsce...