cocos2d-x CCTableView动态的插入删除元素有bug,表现异常。 并且我在使用的时候还想添加动画表现(就像UITableView一样,reloadWithAnimation)。 一开始我以为会很复杂,但是测试后发现需要修改的地方并不是很多。 代码只是满足个人的需求,觉得接口不够漂亮的可以再自行修改。
void insertCellAtIndex(unsigned int idx, float insertDelayTime);// 这个delaytime只是为了跟其他动画配合,可以独立一个函数
int indexFromPoint(int x, int y);
CCPoint pointFromIndex(int index);
void delayInsertCell();
int m_indexToInsert; // 延时插入新数据时备份
cpp文件修改:
void CCTableView::insertCellAtIndex(unsigned int idx, float insertDelayTime)
{
if (idx == CC_INVALID_INDEX)
{
return;
}
unsigned int uCountOfItems = m_pDataSource->numberOfCellsInTableView(this);
if (0 == uCountOfItems || idx > uCountOfItems-1)
{
return;
}
CCTableViewCell* cell = NULL;
int newIdx = 0;
cell = (CCTableViewCell*)m_pCellsUsed->objectWithObjectID(idx);
m_pIndices->clear();
if (cell)
{
newIdx = m_pCellsUsed->indexOfSortedObject(cell);
for (int i = 0; i < (int)newIdx; ++i) {
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
m_pIndices->insert(cell->getIdx());
}
for (unsigned int i=newIdx; i<m_pCellsUsed->count(); i++)
{
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
int ni = cell->getIdx()+1;
this->_setIndexForCell(ni, cell, true);
m_pIndices->insert(ni);
}
}
// [m_pIndices shiftIndexesStartingAtIndex:idx by:1];
if (insertDelayTime > 0) {
m_indexToInsert = idx;
CCDelayTime* delay = CCDelayTime::create(0.2f);
CCCallFunc* call = CCCallFunc::create(this, callfunc_selector(CCTableView::delayInsertCell));
CCSequence* seq = CCSequence::createWithTwoActions(delay, call);
this->runAction(seq);
} else {
//insert a new cell
cell = m_pDataSource->tableCellAtIndex(this, idx);
this->_setIndexForCell(idx, cell, false);
this->_addCellIfNecessary(cell);
this->_updateCellPositions();
this->_updateContentSize();
}
}
void CCTableView::delayInsertCell()
{
CCTableViewCell* cell = m_pDataSource->tableCellAtIndex(this, m_indexToInsert);
this->_setIndexForCell(m_indexToInsert, cell, false);
this->_addCellIfNecessary(cell);
this->_updateCellPositions();
this->_updateContentSize();
}
void CCTableView::removeCellAtIndex(unsigned int idx)
{
if (idx == CC_INVALID_INDEX)
{
return;
}
unsigned int uCountOfItems = m_pDataSource->numberOfCellsInTableView(this);
if (0 == uCountOfItems || idx > uCountOfItems-1)
{
return;
}
unsigned int newIdx = 0;
CCTableViewCell* cell = this->cellAtIndex(idx);
if (!cell)
{
return;
}
newIdx = m_pCellsUsed->indexOfSortedObject(cell);
//remove first
this->_moveCellOutOfSight(cell);
this->_updateCellPositions();
// [m_pIndices shiftIndexesStartingAtIndex:idx+1 by:-1];
// 重新修改indices的值
m_pIndices->clear();
for (int i = 0; i < (int)newIdx; ++i) {
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
m_pIndices->insert(cell->getIdx());
}
for (int i=(int)m_pCellsUsed->count()-1; i >= (int)newIdx; --i)
{
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
int ni = cell->getIdx()-1;
this->_setIndexForCell(ni, cell, true);
m_pIndices->insert(ni);
}
// 补充最后一个元素
if (m_pCellsUsed->count() > 0) {
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(m_pCellsUsed->count() - 1);
int index = cell->getIdx() + 1;
if (index < (int)m_pDataSource->numberOfCellsInTableView(this)) {
// 超出显示范围,更新
this->updateCellAtIndex(index);
// 更新完毕,重新取最后一个cell
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(m_pCellsUsed->count() - 1);
CCPoint dst = cell->getPosition();
cell->setPositionX(dst.x + m_vCellsPositions[index] - m_vCellsPositions[index - 1]);
CCMoveTo* moveTo = CCMoveTo::create(0.2f, dst);
cell->runAction(moveTo);
}
}
}
void CCTableView::_setIndexForCell(unsigned int index, CCTableViewCell *cell, bool animate)
{
if (!cell) {
return;
}
cell->setAnchorPoint(ccp(0.0f, 0.0f));
CCPoint pt = this->_offsetFromIndex(index);
if (animate) {
CCMoveTo* moveTo = CCMoveTo::create(0.2f, pt);
cell->runAction(moveTo);
} else {
cell->setPosition(pt);
}
cell->setIdx(index);
}
int CCTableView::indexFromPoint(int x, int y)
{
CCPoint offset = this->convertToNodeSpace(ccp(x, y));
CCPoint of2 = getContentOffset();
offset.x -= of2.x;
offset.y -= of2.y;
int index = _indexFromOffset(offset);
CCPoint roffset = _offsetFromIndex(index);
CCSize size = m_pDataSource->cellSizeForTable(this);
if (offset.x - roffset.x >= size.width / 2) {
++index;
}
if (index < 0) {
index = 0;
}
int amount = m_pDataSource->numberOfCellsInTableView(this);
if (index > amount) {
index = amount;
}
return index;
}
CCPoint CCTableView::pointFromIndex(int index)
{
CCPoint offset = __offsetFromIndex(index);
CCPoint of2 = getContentOffset();
offset.x += of2.x;
offset.y += of2.y;
CCPoint pt = convertToWorldSpace(offset);
pt = getParent()->convertToNodeSpace(pt);
return pt;
}
一些简单的说明:
1、插入删除元素时,由于没有正确的设置m_pIndices导致各种表现异常。 cocos2d-iphone的代码是正确的,注意代码里面注释掉的一句。 但是移植时偷懒没有处理,并且是一直没有人处理。。。。
2、添加动画表现非常简单,就是setPosition的时候使用CCMoveTo的action。插入时为了和外部动画配合(比如一个拖拽的元素)所以加了一个延时动画,否则插入的元素会立即显示出来,表现不是很良好。
3、pointFromIndex indexFromPoint这两个函数同样是为了导出给外部进行坐标计算的(比如拖拽的元素应该动画移动到什么位置然后消失之类的)。
分享到:
相关推荐
Cocos2d-X CCTableview Cocos2d—X游戏开发之CCTableView详解(十一)(附源码)
资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...
cocos2d-x-2.1.5
在使用cocos2d-x开发游戏的过程中,为了实现逻辑和显示相分离。 在下通宵了一个晚上,写出了该事件类。 谨记,该事件只能用于cocos2d-x中。 事件发送者需要继承EventDispatcher类 事件接收者需要继承EventHandle类...
cocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML文件读取与骨骼动画.rarcocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML...
资源名称:大富翁手机游戏开发实战基于Cocos2d-x3.2引擎内容简介:李德国编著的《大富翁手机游戏开发实战(基于 Cocos2d-x3.2引擎)》使用Cocos2d-x游戏引擎技术,带领读者一步一步从零开始进行大富翁移动游戏的开发...
Cocos2d-x是移动跨平台开发最流行的游戏引擎,而本书是一本很全面的、比较‘接地气’的游戏开发教程。书中汇聚了热门手机游戏《捕鱼达人》开发的实战经验,作者从最基础的内容开始,逐步深入地介绍了Cocos2d-x的相关...
cocos2d-x-3.2下载,不多说。或者可以下载另一个资源 cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5) http://download.csdn.net/download/crazymagicdc/9982656
cocos2d-x 动画工具 Flash2Cocos2d-x 1.3
资源名称:Cocos2D-X游戏开发技术精解内容简介:Cocos2D-X是一款支持多平台的 2D手机游戏引擎,支持iOS、Android、BlackBerry等众多平台。当前,很多移动平台流行的游戏,都是基于Cocos2D-X开发的。 《Cocos2D-X...
经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip
精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发
这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...
《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如何建立跨平台的开发环境;引擎的核心模块——渲染框架;如何实现动态画面和...
Cocos2d-x 3.x游戏开发实战pdf含目录,内容详细,强烈推荐给大家。
Cocos2d-x windows vs2010 配置图文详解
Cocos2d-x实战 JS卷 Cocos2d-JS开发 PDF 电子书完整版本
Cocos2d-x游戏编程——C++篇(电子工业出版社,徐飞 著)书本配套的光盘代码,
cocos2d-x 3.0 人物行走 . 包里有代码和 图片资源.
Cocos2d-x-3.x游戏开发之旅-钟迪龙著 全新pdf版和附书代码(代码为工程文件,可复制) 附带目录标签