本来是想看看网上的教程文章,结果看了好几篇,复制代码各种报错,有很多不存在的类和变量,根本用不了。

所以干脆自己去看官方demo,经过自己分析测试,已经大概会用了,顺便记录一下。

以下是代码,复制粘贴就能运行的那种!(图片资源自备)

新建  tableviewTest.lua,把下面的代码复制进去,保存为utf8格式。在需要的地方(比如main()函数里)调用 require("tableviewTest")

local TableViewTestLayer = class("TableViewTestLayer")
TableViewTestLayer.__index = TableViewTestLayer --这里是为了让layer能调用TableViewTestLayer的方法
function TableViewTestLayer.extend(target)
local t = tolua.getpeer(target)
if not t then
t = {}
tolua.setpeer(target, t)
end
setmetatable(t, TableViewTestLayer)
return target
end --滚动事件
function TableViewTestLayer.scrollViewDidScroll(view)
--print("滚动事件")
end function TableViewTestLayer.scrollViewDidZoom(view)
print("scrollViewDidZoom")
end --cell点击事件
function TableViewTestLayer.tableCellTouched(table,cell)
print("点击了cell:" .. cell:getIdx())
end --cell的大小,注册事件就能直接影响界面,不需要主动调用
function TableViewTestLayer.cellSizeForTable(table,idx)
return ,
end --显示出可视部分的界面,出了裁剪区域的cell就会被复用
function TableViewTestLayer.tableCellAtIndex(table, idx)
local strValue = string.format("%d",idx)
print("数据加载"..strValue)
local cell = table:dequeueCell()
local label = nil
if nil == cell then
print("创建了新的cell")
cell = cc.TableViewCell:new() --添加cell内容
local sprite = cc.Sprite:create("res/tablecell.png")
sprite:setAnchorPoint(cc.p(,))
sprite:setPosition(cc.p(, ))
cell:addChild(sprite) label = cc.Label:createWithSystemFont(strValue, "Helvetica", )
label:setPosition(cc.p(,))
label:setAnchorPoint(cc.p(,))
label:setColor(cc.c3b(,,))
label:setTag()
cell:addChild(label)
else
print("使用已经创建过的cell")
label = cell:getChildByTag()
if nil ~= label then
label:setString(strValue)
end
end return cell
end --设置cell个数,注册就能生效,不用主动调用
function TableViewTestLayer.numberOfCellsInTableView(table)
return
end function TableViewTestLayer:init() local winSize = cc.Director:getInstance():getWinSize() --创建TableView
local tableView = cc.TableView:create(cc.size(,))
--设置滚动方向 水平滚动
tableView:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL)
tableView:setPosition(cc.p(, winSize.height / - ))
tableView:setDelegate()
self:addChild(tableView)
--registerScriptHandler functions must be before the reloadData funtion
--注册脚本编写器函数必须在reloadData函数之前(有道自动翻译) --cell个数
tableView:registerScriptHandler(TableViewTestLayer.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
--滚动事件
tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
--cell点击事件
tableView:registerScriptHandler(TableViewTestLayer.tableCellTouched,cc.TABLECELL_TOUCHED)
--cell尺寸、大小
tableView:registerScriptHandler(TableViewTestLayer.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX)
--显示出可视部分的cell
tableView:registerScriptHandler(TableViewTestLayer.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX)
--调用这个才会显示界面
tableView:reloadData() -----------------------------------------------------------
--跟上面差不多,这里是创建一个“垂直滚动”的TableView
tableView = cc.TableView:create(cc.size(, ))
tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
tableView:setPosition(cc.p(winSize.width - , winSize.height / - ))
tableView:setDelegate()
tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
self:addChild(tableView)
tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
tableView:registerScriptHandler(TableViewTestLayer.tableCellTouched,cc.TABLECELL_TOUCHED)
tableView:registerScriptHandler(TableViewTestLayer.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX)
tableView:registerScriptHandler(TableViewTestLayer.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX)
tableView:registerScriptHandler(TableViewTestLayer.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
tableView:reloadData() return true
end --这里是为了让layer能调用TableViewTestLayer的方法
function TableViewTestLayer.create()
local layer = TableViewTestLayer.extend(cc.Layer:create())
if nil ~= layer then
layer:init()
end return layer
end --运行测试场景
function runTableViewTest()
local newScene = cc.Scene:create()
local newLayer = TableViewTestLayer.create()
newScene:addChild(newLayer)
cc.Director:getInstance():replaceScene(newScene)
--return newScene
end runTableViewTest() return TableViewTestLayer

(原创)cocos2dx-lua TableView官方demo分析的更多相关文章

  1. [原创]cocos2d-x + Lua接入iOS原生SDK的实现方案

    相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...

  2. RobotFramework 官方demo Quick Start Guide rst配置文件分析

    RobotFramework官方demo Quick Start Guide rst配置文件分析   by:授客 QQ:1033553122     博客:http://blog.sina.com.c ...

  3. Cocos2d-x Lua 阅读Csv文件,使用数据更方便

    在我的书或出售之前,我的源代码,有Csvshadow文件. 也许这是偏见.我与工作将是最长的轮廓Csv,所以,我会帮助不大喜欢它的游戏. Csv文件,非常格式easy,也就是说,一个数据线,字段之间用 ...

  4. cocos2d-x + Lua接入iOS原生SDK的实现方案[转]

    相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...

  5. Cocos2dx lua 3D实例代码

    用cocoside 创建一个项目 cocos2dx lua 项目即可 ,然后替换掉gamescene 就可以,具体效果还有函数的参数,相信大家一看就明白.简单说下ide 创建的 cocos lua 项 ...

  6. cocos2d-x for android:SimpleGame分析

    cocos2d-x for android:SimpleGame分析 作为cocos2d-x的标配DEMO,SimpleGame可算是给入门学cocos2d-x的俺们这些新手门学习的对象了,那么来分析 ...

  7. Unity 官方 Demo: 2DPlatformer 的 SLua 版本。

    9月份时,趁着国庆阅兵的假期,将 Unity 官方 Demo: 2DPlatformer 移植了一个 SLua 版本,并放在了我的 GitHub 账号下:https://github.com/yauk ...

  8. cocos2d-x lua脚本开发 1

    自从开始关注OpenResty之后,逐渐关注Lua语言,发现这个语言真真是容易让人喜爱的语言.偶然间发现了cocos2d-x,还支持lua,所以果断尝试一下. 这里是在cocos2d-x官方网站下载了 ...

  9. Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors

    在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...

随机推荐

  1. 页面跳转时,url 传大数据的参数不全的问题+序列化对象

    1.页面跳转时,url 传大数据的参数不全的问题 //传参: url: '/pages/testOfPhysical/shareEvaluation?detailInfo=' +encodeURICo ...

  2. (haut oj 1261 ) 地狱飞龙 利用不定积分求值

    题目链接:http://218.28.220.249:50015/JudgeOnline/problem.php?id=1261 题目描述 最近clover迷上了皇室战争,他抽到了一种地狱飞龙,很开心 ...

  3. 家庭记账本小程序之查(java web基础版六)

    实现查询消费账单 1.main_left.jsp中该部分,调用search.jsp 2.search.jsp,提交到Servlet中的search方法 <%@ page language=&qu ...

  4. Kubernetes — 控制器

    Pod 这个看似复杂的 API 对象,实际上就是对容器的进一步抽象和封装而已. 说得更形象些,“容器”镜像虽然好用,但是容器这样一个“沙盒”的概念,对于描述应用来说, 还是太过简单了. 这就好比,集装 ...

  5. SpringBoot使用Filter过滤器处理是否登录的过滤时,用response.sendRedirect()转发报错

    1.使用response.sendRedirect("/login")时报错,控制台报错如下: Cannot call sendError() after the response ...

  6. Spring Boot的web开发

    web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 自动配置的ViewResolver 视图的 ...

  7. Python——Flash框架——用户认证

    一.认证扩展 1.Flask-Login:管理已登录用户的用户会话 2.Werkzeug:计算几码散列值并进行核对 3.itsdangerous:生成并核对加密安全令牌 二.Werkzeug gene ...

  8. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  9. Js元素拖拽功能实现

    Js元素拖拽功能实现 需要解决的问题 最近项目遇到了一个问题,就是用户某个操作需要弹出一个自定义的内容输入框,但是有个缺点,当浏览太大的时候没办法点击确认和取消按钮,应为这个弹出框是采用绝对定位的,取 ...

  10. redis持久化和主从同步

    redis持久化rdb与aof 简介 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的 ...