本方法只能在CCTableView的扩展类中实现,因为其中的计算方法依赖于CCTableView的protected方法。
具体代码如下:
void scrollToCellIndex(int index) { CCPoint offset = ccpSub(CCPointZero, _offsetFromIndex(index)); float newX, newY; CCPoint maxInset, minInset; maxInset = this->maxContainerOffset(); minInset = this->minContainerOffset(); //check to see if offset lies within the inset bounds newX = MIN(offset.x, maxInset.x); newX = MAX(newX, minInset.x); newY = MIN(offset.y, maxInset.y); newY = MAX(newY, minInset.y); setContentOffset(ccp(newX, newY), true); }
补充:
后来参考其他博文,发现Direction和VerticalFillOrder其实是开放出来的,可以自行计算得到,而不是一定要通过protected function依赖的。
贴上lua方法:
-- 追加滚动到指定位置的方法 -- dataSource需要从外部传入,因为quick-cocos2d-x的CCTableView.toua中没有提供 CCTableViewDataSource* getDataSource() local dataSource = params.dataSource t.scrollToCellIndex = function(self, index) local direction = self:getDirection() local verticalFillOrder = self:getVerticalFillOrder() local offsetX = 0 local offsetY = 0 -- 单元格大小 local cellSize = dataSource:cellSize(self) -- 总数 local itemCounts = dataSource:numberOfCells(self) -- 根据方向计算偏移量 if direction == kCCScrollViewDirectionVertical then if verticalFillOrder == kCCTableViewFillTopDown then offsetY = -cellSize.height * (itemCounts - index - 1) else offsetY = -cellSize.height * index; end else if verticalFillOrder == kCCTableViewFillTopDown then offsetX = -cellSize.width * (itemCounts - index - 1) else offsetX = -cellSize.width * index end end -- 检查偏移量是否越界 local maxInset = self:maxContainerOffset(); local minInset = self:minContainerOffset(); offsetX = math.min(offsetX, maxInset.x); offsetX = math.max(offsetX, minInset.x); offsetY = math.min(offsetY, maxInset.y); offsetY = math.max(offsetY, minInset.y); self:setContentOffset(ccp(offsetX, offsetY), true); end
参考博文:http://blog.csdn.net/wzq9706/article/details/9105915
相关推荐
Cocos2d-X CCTableview Cocos2d—X游戏开发之CCTableView详解(十一)(附源码)
绑定测试 主要将cocos2d-x里面的CCTableView控件绑定成JS可以直接调用的方式
Cocos2d-x CCTableView使用详解
CCTableView CCArry ,CCArry中有坑,需要填一下。
cocos2d-x CCScrollView和CCTableView的使用 实例源码,详情请移步:http://codingnow.cn/cocos2d-x/1024.html
给CCTableView的单元格设置不同的尺寸。
对cocos2d-js中CCTableView的简易封装,可直接使用,包含用例,数据源可使用Array和object,支持cell间距等若干属性的定制,支持每个cell里包含多个元素。优化了滑动过程中刷新时得抖动现象
cocos2d-x 实现 ,其中用到 ccscrollview cctableview 等,及自己写的 饼状图控件
UI部分一直是cocos2d-x的弱项,而这种境况在3.0版本中将得到改善。让我们从一个曾经的坑入手——CCTableView,来看看这些新特性。相关博客:http://blog.csdn.net/fansongy/article/details/16950241
cocos2d-x 游戏关卡选择的设计 cocos2s-x v2.2.0