最近在写排行榜的时候,要做成下拉或上拉添加新的Item,下拉的时候通过onscroll的这个cocos自己封装的方法中getinnerPositionY来检测但是在上拉的时候会出现问题,但是scrollView可以解决这个问题。
1.创建TableView
--@function 创建tableView
function MainScene:createTableView()
--滑动调用
local function scrollViewDidScroll(view)
--print(view:getViewSize().height)
--print(view:minContainerOffset().y)
--print(view:getContentOffset().y)
end
--缩放调用
local function scrollViewDidZoom(view)
end
--触摸时调用
local function tableCellTouched(view, cell)
print("触摸到的是"..cell:getIdx())
end
--细胞的大小
local function cellSizeForTable(view, index)
return 100, 100
end
--细胞内的具体内容
local function tableCellAtIndex(view, index)
local size = cc.Director:getInstance():getVisibleSize()
local cell = view:dequeueCell()
local strValue = string.format("%d", index)
local label = nil
if nil == cell then
cell = cc.TableViewCell:new()
label = cc.Label:createWithSystemFont(strValue, "Helvetica", 20)
label:setAnchorPoint(cc.p(0, 0))
label:setPosition(cc.p(cc.p(size.width / 2, 0)))
label:setTag(10)
cell:addChild(label)
else
label = cell:getChildByTag(10)
if nil ~= label then
label:setString(strValue)
end
end
return cell
end
--细胞的数量
local function numberOfCellInTableView()
return 25
end
--创建tableView
local size = cc.Director:getInstance():getVisibleSize()
local tableView = cc.TableView:create(cc.size(size.width, size.height - 100))
tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
tableView:setAnchorPoint(cc.p(0, 0))
tableView:setPosition(cc.p(0, 50))
tableView:setDelegate()
tableView:registerScriptHandler(numberOfCellInTableView, cc.NUMBER_OF_CELLS_IN_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:reloadData()
tableView:addTo(self)
end
2.创建ListView
--@function createListView()
function MainScene:createListView()
local size = cc.Director:getInstance():getVisibleSize()
local listView = ccui.ListView:create()
listView:setDirection(ccui.ScrollViewDir.vertical)
listView:setItemsMargin(0)
listView:setScrollBarEnabled(true)
listView:setContentSize(cc.size(size.width / 2, size.height / 2))
listView:setAnchorPoint(cc.p(0, 0))
listView:setPosition(cc.p(display.cx / 2, display.cy / 2))
for i = 1, 10 do
local imageView = ccui.ImageView:create("soil.png")
imageView:ignoreContentAdaptWithSize(false)
imageView:setAnchorPoint(cc.p(0,0))
imageView:setPosition(cc.p(0,0))
imageView:setScale9Enabled(true)
imageView:setContentSize(cc.size(size.width / 2, 150))
--imageView:setTouchEnabled(true)
label = cc.Label:createWithSystemFont(i, "Helvetica", 20)
label:setAnchorPoint(cc.p(0, 0))
label:setPosition(cc.p(cc.p(0, 0)))
local layout = ccui.Layout:create()
layout:setBackGroundImageScale9Enabled(true)
layout:setContentSize(cc.size(size.width / 2, 200))
layout:setBackGroundColor(cc.c3b(100, 200, 100))
layout:setBackGroundImage("stone.png")
layout:setAnchorPoint(cc.p(0,0))
layout:setTouchEnabled(true)
layout:addChild(imageView)
layout:addChild(label)
listView:pushBackCustomItem(layout)
--listView:add(layout)
end
local function listViewEvent(sender, eventType)
if eventType == ccui.ListViewEventType.ONSELECTEDITEM_START then
print(sender:getCurSelectedIndex())
end
end
local function scrollViewEvent(sender, eventType)
if eventType == ccui.ScrollviewEventType.scrollToBottom then
print("滑到底部了")
elseif eventType == ccui.ScrollviewEventType.scrollToTop then
print("滑到顶部了")
end
end
listView:addScrollViewEventListener(scrollViewEvent)
listView:addEventListener(listViewEvent) --不设置交互性(setTouchEnabled)不可以使用的哦
self:addChild(listView)
end
3.创建二级菜单
function MainScene:createSecondMenus()
local size = cc.Director:getInstance():getVisibleSize()
local listView = ccui.ListView:create()
listView:setDirection(ccui.ScrollViewDir.vertical)
listView:setItemsMargin(0)
listView:setScrollBarEnabled(true)
listView:setContentSize(cc.size(size.width / 2, size.height / 2))
listView:setAnchorPoint(cc.p(0, 0))
listView:setPosition(cc.p(display.cx / 2, display.cy / 2))
for i = 1, 4 do
local imageView = ccui.ImageView:create("soil.png")
imageView:ignoreContentAdaptWithSize(false)
imageView:setAnchorPoint(cc.p(0,0))
imageView:setPosition(cc.p(0,0))
imageView:setScale9Enabled(true)
imageView:setContentSize(cc.size(size.width / 2, 150))
label = cc.Label:createWithSystemFont(i, "Helvetica", 20)
label:setAnchorPoint(cc.p(0, 0))
label:setPosition(cc.p(cc.p(0, 0)))
local layout = ccui.Layout:create()
layout:setName("layout"..i)
layout:setBackGroundImageScale9Enabled(true)
layout:setContentSize(cc.size(size.width / 2, 200))
layout:setBackGroundColor(cc.c3b(100, 200, 100))
layout:setBackGroundImage("stone.png")
layout:setAnchorPoint(cc.p(0,1))
layout:setTouchEnabled(true)
layout:addChild(imageView)
layout:addChild(label)
listView:pushBackCustomItem(layout)
end
local function listViewEvent(sender, eventType)
if eventType == ccui.ListViewEventType.ONSELECTEDITEM_END then
local index = sender:getCurSelectedIndex() + 1
--print(index)
local layoutItem = listView:getChildByName("layout"..index)
if layoutItem:getContentSize().height == 200 then
layoutItem:setContentSize(cc.size(size.width / 2, 300))
listView:forceDoLayout()
listView:jumpToPercentVertical((index / 4) * 100)
else
layoutItem:setContentSize(cc.size(size.width / 2, 200))
listView:forceDoLayout()
listView:jumpToPercentVertical((index / 4) * 100)
end
end
end
listView:addEventListener(listViewEvent) --不设置交互性(setTouchEnabled)不可以使用的哦
self:addChild(listView)
end
4.table读取值
function checkTableAndLog(tableName)
local countNumber = 1
local strSpace = " "
local info = ""
if type(tableName) ~= "table" then
error("错误的参数")
return
end
local function checkInnerTable(innerTable)
countNumber = countNumber + 1
strSpace = strSpace..strSpace
if type(innerTable) == "table" then
for innerKey, innerValue in pairs(innerTable) do
if type(innerValue) ~= "table" then
print(strSpace..countNumber.." key:"..innerKey.." value:"..innerValue)
else
print(strSpace..countNumber.." key:"..innerKey.." value:table")
checkInnerTable(innerValue)
end
end
end
countNumber = 1
strSpace = " "
end
for key, value in pairs(tableName) do
if type(value) == "table" then
print(strSpace..countNumber.." key:"..key.." value:table")
checkInnerTable(value)
else
print(strSpace..countNumber.." key:"..key.." value:"..value)
end
end
end
a = {1,{2, 22, {3, 33}}, 1111, {44, 44}}
checkTableAndLog(a)