TableView+Button
local MainScene = class("MainScene", cc.load("mvc").ViewBase) function MainScene:onCreate() local im = ccui.ImageView:create("HelloWorld.png") self:addChild(im) im:setPosition(200,200) local scene = self local list = {} for i=1,20 do table.insert(list,i.."号位") end dump(list) local tableSize = cc.size(500,800) local cellSize = cc.size(tableSize.width, 100) local tv = cc.TableView:create(tableSize) self:addChild(tv) tv:setPosition(500,300) tv:setScale(1.5) local function tableCellAtIndex(table, idx) --print("tableCellAtIndex"..idx) local cell = table:dequeueCell() if cell then --cell:removeAllChildren() else cell = cc.TableViewCell:create() local ly = ccui.Layout:create() ly:setBackGroundColorType(1) ly:setBackGroundColor({r = 255, g = 0, b = 0}) ly:setBackGroundColorOpacity(102) local layout = ccui.LayoutComponent:bindLayoutComponent(ly) layout:setSize({width = cellSize.width,height =cellSize.height-2}) cell:addChild(ly) cell.oneItem= ccui.Text:create("","",30) cell.oneItem:setFontSize(30) cell.oneItem:setfont cell.oneItem:setColor(cc.c3b(0,0,0)) cell:addChild(cell.oneItem,2) cell.oneItem:setPosition(50,50) cell.btn = ccui.Button:create("btn.png") cell.btn:setAnchorPoint(cc.p(0,0)) cell:addChild(cell.btn,1) cell.btn:setSwallowTouches(false) local function btnClick(sender, state) --print("btn "..cell.btn:getTag()) local event = {} if state == 0 then event.name = "began" tv.ScorllState = false elseif state == 1 then event.name = "moved" elseif state == 2 then event.name = "ended" if tv.ScorllState == false then print("btn click "..cell.btn:getTag()) end else event.name = "cancelled" end print(event.name) end --cell.btn:addClickEventListener(btnClick) cell.btn:addTouchEventListener(btnClick) end cell.btn:setTag(idx+1) cell.oneItem:setString(list[idx+1]) return cell end local function cellSizeForIndex(table, idx) --print("cellSizeForIndex"..idx) return cellSize.width, cellSize.height end local function numberOfCellsInTableView(table) --print("numberOfCellsInTableView") return #list end local function tableDidScroll(table) --print("tableDidScroll") tv.ScorllState = true end tv:setDelegate() tv:registerScriptHandler(cellSizeForIndex, cc.TABLECELL_SIZE_FOR_INDEX) tv:registerScriptHandler(tableCellAtIndex, cc.TABLECELL_SIZE_AT_INDEX) tv:registerScriptHandler(numberOfCellsInTableView, cc.NUMBER_OF_CELLS_IN_TABLEVIEW) tv:registerScriptHandler(tableDidScroll, cc.SCROLLVIEW_SCRIPT_SCROLL) tv:reloadData() end return MainScene