cocos2d-x-LuaProxy学习日志(4) -- TableView使用

TableView


      在cocos2d-x中集成了一些扩展插件,这些引用文件都可以在cocos2d-x目录下的extentions下找到,其中常用的scrollView和tableView的引用文件在GUI目录下。若想深入理解scrollView和tableView,可参考这两篇博文:cocos2d-x学习日志(7) --CCScrollView和CCTableView的使用 和 cocos2d-x 菜鸟学习笔记七(界面控件之scrollView与tableView)

      但本教程,不详细介绍了。直接上代码:

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
print("----------------------------------------")
print("LUA ERROR: " .. tostring(msg) .. "\n")
print(debug.traceback())
print("----------------------------------------")
end

local function main()
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)

local cclog = function(...)
print(string.format(...))
end

require "hello2"
cclog("result is " .. myadd(3, 5))

---------------

local function createTest()
	local TestLayer = CCLayer:create()

	local proxy = LuaProxy:create()
	proxy:retain() -- hold the proxy, while the button gone, release the proxy.
	local n = proxy:readCCBFromFile("ccbResources/ccb/Test.ccbi") -- Got a CCNode
	local l = tolua.cast(n, "CCLayer") -- Cast the node into CCLayer
	TestLayer:addChild(l)
	
	--绑定一个node节点
	local NodeBg = tolua.cast(proxy:getNode"NodeBg","CCNode")
	
	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,30) --每格的尺寸
		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("", "Arial", 20), 0, 1)
				
				--这里就相当cell容器,可以装入ccbi层、精灵、菜单等控件
				local spriteDemo = CCSprite:create("btn-back-0.png")
				spriteDemo:setAnchorPoint(ccp(0,0))
				
				a2:addChild(spriteDemo)
				
			end
				-- 修改“格”对象的内容
				tolua.cast(a2:getChildByTag(1), "CCLabelTTF"):setString(data[a1 + 1])
				r = a2
				
		elseif fn == "numberOfCells" then
					r = #data
					
		-- Cell events:
		-- 表格事件:			
		elseif fn == "cellTouched" then			-- A cell was touched, a1 is cell that be touched. This is not necessary.
		elseif fn == "cellTouchBegan" then		-- A cell is touching, a1 is cell, a2 is CCTouch
		elseif fn == "cellTouchEnded" then		-- A cell was touched, a1 is cell, a2 is CCTouch
		elseif fn == "cellHighlight" then		-- A cell is highlighting, coco2d-x 2.1.3 or above
		elseif fn == "cellUnhighlight" then		-- 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
			
		end		
		return r 
	
	end)
	--创建tableview
	local t = LuaTableView:createWithHandler(h,CCSizeMake(480,200))
	t:setBounceable(true)
	
	--节点定位tableview
	NodeBg:addChild(t,1)
	
	return TestLayer
end



-- run
local sceneGame = CCScene:create()
	sceneGame:addChild(createTest())
	CCDirector:sharedDirector():runWithScene(sceneGame)
end

xpcall(main, __G__TRACKBACK__)

效果图:



转载请注明出处:http://blog.csdn.net/rexuefengye/article/details/16355571

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值