oDustGgg個人原創、歡迎轉載、轉載請註明出處、http://blog.csdn.net/odustggg/article/details/8171230
一、建立列表層
function create_listview()local layer = CCLayerColor:layerWithColorWidthHeight(ccc4(255, 0, 0, CORLORLAYERENABLE),640, 200)layer:setAnchorPoint(CCPointMake(0,0))layer:setIsRelativeAnchorPoint(true) --anchorPoint這個屬性雖然是屬於CCNode的、但CCLayer設定anchorPoint沒有效果、--CCLayer的anchorPoint被預設設定在(0, 0)、在setAnchorPoint之前要先設定setIsRelativeAnchorPoint = truelayer:setPosition(PT(165,120))-- 兩種排列 CCListViewModeHorizontal 水平 CCListViewModeVertical 豎直 m_pList = CCListView:viewWithMode(CCListViewModeHorizontal) -- 初始化控制項ListView m_pList:setContentSize(CCSize(640,200)) m_pList:setDelegateName('testListView') m_pList:setAnchorPoint(CCPointMake(0,0))m_pList:setPosition(PT(0,0)) --兩種分隔CCListViewCellSeparatorStyleNone CCListViewCellSeparatorStyleSingleLine m_pList:setSeparatorStyle(CCListViewCellSeparatorStyleNone) layer:addChild(m_pList) return layerend二、實現的方法
--返回有多少頁
function testListView_CCListView_numberOfCells(listview,data) data.nNumberOfRows= listview:size() end
--listview在滑動中
function testListView_triggerDidScrollToRow(nRow) local i = nRow cclog("scrolling:"..i) end
--cell被選中
function testListView_triggerDidClickCellAtRow(nRow) local i = nRow cclog("clicked:"..i) end
--將listview層的觸摸開啟
function setTouch_openLayer(layer) layer:registerScriptTouchHandler(onTouch_openLayer,nil,-129,nil) layer:setIsTouchEnabled(true)end
--具體顯示什麼內容,每一頁的內容
function testListView_triggerCellForRow(listview,data) m_nCurrnetPage = data.nRow + 1 --data.nRow從0開始 local cell = CCListViewCell:node() cell:setOpacity(0) cell:setContentSize(m_pList:getContentSize()) cell:setSelectionColor(ccc4(0, 0, 0, 0)) data.cell = cell _curCell = cell local listItemSize = CCSize:new(m_pList:getContentSize().width , m_pList:getContentSize().height) for n = 1, data.nNumberOfRows do cell:addChild(xxxxxx) end end
三、listview的使用
listview_layer = create_listview()_layer:addChild(listview_layer,2)setTouch_openLayer(listview_layer)