加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx lua tableview

发布时间:2020-12-14 19:10:53 所属栏目:百科 来源:网络整理
导读:function tableview() local origin = CCEGLView:sharedOpenGLView():getVisibleSize(); local TestLayer = CCLayer:create() local NodeBg=CCNode:create(); NodeBg:setPosition(ccp(10,origin.height/8)) local data = {} for i = 1,10 do table.insert(da

function tableview()

local origin = CCEGLView:sharedOpenGLView():getVisibleSize();

local TestLayer = CCLayer:create()

local NodeBg=CCNode:create();

NodeBg:setPosition(ccp(10,origin.height/8))

local data = {}

for i = 1,10 do table.insert(data,"Data"..i) end --装入tableview的数据源

local h = LuaEventHandler:create(function(fn,table,a1,a2)

local r

if fn == "cellSize" then

r = CCSizeMake(480,100) --每格的尺寸

elseif fn == "cellAtIndex" then

-- 请求“格”对象,a1是格索引(从0开始),a2是缓存的格对象(可能为空)

-- 在此建立“格”对象并填充其要显示的内容。

if not a2 then

a2 = CCTableViewCell:create()

-- Build cell struct,just like load ccbi or add sprite

-- 建立“格”对象的结构,不要在此设定其显示的内容。

a2:addChild(CCLabelTTF:create(""..a1,"Arial",20),1)

--这里就相当cell容器,可以装入ccbi层、精灵、菜单等控件

local spriteDemo = CCScale9Sprite:create("80.png");

spriteDemo:setAnchorPoint(ccp(0,0))

spriteDemo:setContentSize(CCSizeMake(300,100))

a2:addChild(spriteDemo)

end

-- 修改“格”对象的内容

tolua.cast(a2:getChildByTag(1),"CCLabelTTF"):setString(data[a1 + 1])

r = a2

a2:setTag(a1+1);

elseif fn == "numberOfCells" then

r = #data

-- Cell events:

-- 表格事件:

elseif fn == "cellTouched" then

print("按了:".. a1:getTag()) -- A cell was touched,a1 is cell that be touched. This is not necessary.

elseif fn == "cellTouchBegan" then

print("按下".. a1:getTag()) -- A cell is touching,a1 is cell,a2 is CCTouch

elseif fn == "cellTouchEnded" then

print("按完".. a1:getTag()) -- A cell was touched,a2 is CCTouch

elseif fn == "cellHighlight" then

print("高亮的:"..a1:getTag() ) -- A cell is highlighting,coco2d-x 2.1.3 or above

elseif fn == "cellUnhighlight" then

print("不是高亮".. a1:getTag()) -- A cell had been unhighlighted,coco2d-x 2.1.3 or above

elseif fn == "cellWillRecycle" then -- A cell will be recycled,coco2d-x 2.1.3 or above

print( a1:getTag() )

end

return r

end)

--创建tableview

local t = LuaTableView:createWithHandler(h,CCSizeMake(480,400))

t:setBounceable(true)

--节点定位tableview

NodeBg:addChild(t,1)

TestLayer:addChild(NodeBg)

-- parent:addChild(TestLayer)

return TestLayer

end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读