cocos2d-x lua tableView

cocos2d-x技术群新群:117871561
c++技术交流群:593010226

我项目里用的这个鬼,没有用scroolview,是因为 这个鬼可以重复利用创建的子项,这样不需要重复的创建 与渲染 子项 。用着会比较流畅

第一部分 tableview基本设置

--创建tableview
self._skillView = cc.TableView:create(size)
----设置滚动方向  SCROLLVIEW_DIRECTION_VERTICAL是垂直滚动   SCROLLVIEW_DIRECTION_HORIZONTAL 是水平滚动
self._skillView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)    
  --竖直从上往下排列  
self._skillView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
self._skillView:setPosition(cc.p(50, 10))
--设置代理
self._skillView:setDelegate()
skill_bg:addChild(self._skillView)

第二部分tableview 添加回调 常用的五个个回调函数

  self.m_tableView:registerScriptHandler( handler(self, self.scrollViewDidScroll),cc.SCROLLVIEW_SCRIPT_SCROLL);           --滚动时的回掉函数  
    self.m_tableView:registerScriptHandler( handler(self, self.cellSizeForTable), cc.TABLECELL_SIZE_FOR_INDEX);             --列表项的尺寸  
   self.m_tableView:registerScriptHandler( handler(self, self.tableCellAtIndex), cc.TABLECELL_SIZE_AT_INDEX);              --创建列表项  
   self.m_tableView:registerScriptHandler( handler(self, self.numberOfCellsInTableView), cc.NUMBER_OF_CELLS_IN_TABLEVIEW); --列表项的数量  
self.m_tableView:registerScriptHandler( handler(self, self.tableCellTouched), cc.TABLECELL_TOUCHED); --触摸列表项cell的回调
--加载tableView的所有列表数据
      self.m_tableView:reloadData();  

第三部分tableview 回调的实现

--触摸子项时的点击事件 我的项目里没有写点击响应
function PriGameLayer:scrollViewDidScroll(view)
end
--滚动时点击事件我的项目里没有写点击响应
function PriGameLayer:scrollViewDidScroll(view)
end
--子视图大小 view是tableview,idx是每个子项的索引
function PriGameLayer:cellSizeForTable(view, idx)
    return 200 , 23
end

--子视图数目  返回的个数是tableview的高度/单个子项的高度+1
function PriGameLayer:numberOfCellsInTableView(view)
     if self._gameLayer.curCount == 6 then 
         return self._gameLayer.curCount+1
     else
        return self._gameLayer.curCount
    end
end
--获取子视图
function PriGameLayer:tableCellAtIndex(view, idx)   
    local index = idx+1
    local cell = view:dequeueCell()
        if not cell then
           cell = cc.TableViewCell:new()
            cc.Label:createWithTTF("Score","fonts/round_body.ttf",22)
            :move(80,10)
            :setTag(5)
            :setColor(cc.c3b(34,139,34))
            :setAnchorPoint(cc.p(0.0, 0.5))
            :addTo(cell)
            
        end 
           if self.playerScore == 1 then
               local str = ""
               if self._gameLayer.Player_1[index] > 0 then
                   str = "第"..index.."局: ".."+"..self._gameLayer.Player_1[index]
                else
                   str = "第"..index.."局: "..self._gameLayer.Player_1[index]
               end
             cell:getChildByTag(5):setString(str)
           elseif self.playerScore == 2 then 
                local str = ""
               if self._gameLayer.Player_2[index] > 0 then
                   str = "第"..index.."局: ".."+"..self._gameLayer.Player_2[index]
                else
                   str = "第"..index.."局: "..self._gameLayer.Player_2[index]
               end
             cell:getChildByTag(5):setString(str)
        elseif self.playerScore == 3 then 
              local str = ""
               if self._gameLayer.Player_3[index] > 0 then
                   str = "第"..index.."局: ".."+"..self._gameLayer.Player_3[index]
               else
                   str = "第"..index.."局: "..self._gameLayer.Player_3[index]
               end
             cell:getChildByTag(5):setString(str)
        end 
        
    return cell
end




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,403评论 25 708
  • 前言 我选择开发一个游戏有很多原因。我觉得自己是“核心”玩家,过去的大部分时间我都花在玩游戏,自己制作、阅读和游戏...
    月影檀香阅读 12,072评论 1 27
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,378评论 0 1
  • 亲爱的晋善: 新年快乐,今天是大年初一,农历新年的第一天,每一个月的第一天都是初一,中国人是相当重视这样的第一天,...
    丁爸阅读 622评论 0 51
  • 抗风名仕古今闻,防沙抵碱更属君。 点缀家园闲不了,犹公技挂数片云。 注:木麻黄,生长迅速,抗风力强,不怕沙埋,能耐...
    万川山人阅读 300评论 0 1