今天主要讲解一下ScrollView和TableView控件的使用,话不多说,直接贴代码:
1、ScrollView:
local scrollView = cc.ScrollView:create()
-- local function scrollViewDidZoom()
-- print("ScrollViewDidZoom")
-- end
-- local function scrollViewDidScroll()
-- print("ScrollViewDidScroll")
-- end
local layerColor = cc.LayerColor:create(cc.c4b(100,100,100),700,600)
scrollView:setViewSize(cc.size(700,300))
scrollView:setContainer(layerColor)
layerColor:setPosition(cc.p(0,0))
scrollView:setPosition(cc.p(50,50))
bg:addChild(scrollView)
scrollView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)--垂直滚动
--cc.SCROLLVIEW_DIRECTION_HORIZONTAL 水平滚动
--cc.SCROLLVIEW_DIRECTION_BOTH 垂直水平滚动
scrollView:setBounceable(true)
--scrollView:setDelegate()
--scrollView:registerScriptHandler(scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
--scrollView:registerScriptHandler(scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
for i = 1,6 do
localstrFmt = string.format("第%d行",i)
locallabel = cc.Label:createWithSystemFont(strFmt,"Arial",32)
label:setColor(cc.c3b(255,0))
label:setPosition(cc.p(350,600-i*100+50))
layerColor:addChild(label)
end
2、TableView
local function scrollViewDidScroll(view) print("ScrollViewDidScroll") end local function scrollViewDidZoom(view) print("ScrollViewDidZoom") end local function tableCellTouched(table,cell) print("cell touched at index: "..cell.getIdx()) end local function cellSizeForTable(table,idx) return 60,60 end local function tableCellAtIndex(table,idx) local strValue = string.format("%d",idx) local cell = table:dequeueCell() local label = nil if nil == cell then cell = cc.TableViewCell:create() label = cc.Label:createWithSystemFont(strValue,32) label:setPosition(cc.p(0,0)) label:setAnchorPoint(cc.p(0,0)) cell:addChild(label) label:setTag(123) else label = cell:getChildByTag(123) if label ~= nil then label:setString(strValue) end end return cell end local function numberOfCellsInTableView(table) return 20 end local tableView = cc.TableView:create(cc.size(700,300)) tableView:setPosition(cc.p(50,40)) tableView:setDelegate() tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL) bg:addChild(tableView) tableView:registerScriptHandler(scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) tableView:registerScriptHandler(scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) tableView:registerScriptHandler(tableCellTouched,cc.TABLECELL_TOUCHED) tableView:registerScriptHandler(cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX) tableView:registerScriptHandler(tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX) tableView:registerScriptHandler(numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW) tableView:reloadData()
关于ScrollView和TableView的使用大概就是这样,会用就可以了。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|