CCTableView如果滚动到指定的行?

比如要通过程序让CCTableView滚动到第12行,这个如何做到?

之前的CCListView很简单,就是SelectCellAtRow就可以了,那么CCtableView呢?

  • 本帖最后由 lionqun 于 2013-3-7 20:22 编辑 *

使用时:
m_pTableView->setContentOffset(ccp(0, DTUtility::calculateTableOffset(360, 60, m_pNearbyInfos->count(), 0)));
我这里是定位到首行

  • 本帖最后由 lionqun 于 2013-3-7 20:21 编辑 *

我专门写了一个方法,用来确定CCTableView ContentOffset的位置
int DTUtility::calculateTableOffset(int intViewHeight, int intCellHeight, int intCellCount, int intCellIndex)
{
int intTableTotalHeight = intCellHeight * intCellCount;
if(intTableTotalHeight > intViewHeight){
//Negative
return 0 - (intCellCount - (intCellIndex + 1)) * intCellHeight;
}
else{
//Positive
return intViewHeight - intTableTotalHeight;
}
}
说明:
intViewHeight 是CCTableView的高度
intCellHeight 是单元格的高度
intCellCount 是你的总的单元格数量
intCellIndex 是滚动到单元格的索引

同问,我对这个控件表示很无语,那个函数一直不知道有啥用

得到那行的偏移,再setcontentoffset就行了!

好像是不行啊,setcontentoffset这个方法没有用,而且我每次拖动cell,然后取得getContentOffset,但是每次得到的都是0,这是为什么?

我看看啥时候能有人回答这个问题?

今天再顶一次!

这个简单的问题,一天了没人回答。。。。人气何在?

static void scrollToIndex(CCTableView * m_table, int index){
auto cellHeight = m_table->getContentOffset().y;

m_table->setContentOffset(ccp(0,
                              cellHeight
                              + m_table->getDataSource()->tableCellSizeForIndex(m_table, 0).height
                              * 10));

}

listView->scrollCellToBack(20, true);

void scrollToIndex(int index)
{
// index不对不操作
if (index < 0 || index > numberOfCellsInTableView(mTableView) - 1)
return;

{
	// 保存所有的位置数据
	std::vector<float> m_vCellsPositions;
	{
		int cellNum = numberOfCellsInTableView(mTableView);
		 m_vCellsPositions.resize(cellNum + 1, 0.0);

		if (cellNum > 0)
		{
			float currentPos = 0;
			CCSize cellSize;
			for (int i=0; i < cellNum; i++)
			{
				m_vCellsPositions* = currentPos;
				cellSize = tableCellSizeForIndex(mTableView, i);
				switch (mTableView->getDirection())
				{
				case kCCScrollViewDirectionHorizontal:
					currentPos += cellSize.width;
					break;
				default:
					currentPos += cellSize.height;
					break;
				}
			}
			m_vCellsPositions = currentPos;//1 extra value allows us to get right/bottom of the last cell
		}
	}
	

	//  __offsetFromIndex
	CCPoint offset;
	{
		//CCSize  cellSize;
		switch (mTableView->getDirection())
		{
		case kCCScrollViewDirectionHorizontal:
			offset = ccp(m_vCellsPositions, 0.0f);
			break;
		default:
			offset = ccp(0.0f, m_vCellsPositions);
			break;
		}
	}
	


	// _offsetFromIndex
	const CCSize curr_cellSize = tableCellSizeForIndex(mTableView, index);
	if (mTableView->getVerticalFillOrder() == kCCTableViewFillTopDown)
	{
		offset.y = mTableView->getContainer()->getContentSize().height - offset.y - curr_cellSize.height;
	}

	// 移动container,把cell放在最上面
	offset.y = -offset.y;		// cell在y处,那么container就去要移动-y才可以看得到
	// 设置位置,此时offset是刚好在最下面,需要移动到最上面
	offset.y = offset.y + mTableView->getViewSize().height - curr_cellSize.height;

	if (offset.y > mTableView->maxContainerOffset().y)
	{
		offset.y = mTableView->maxContainerOffset().y;
	}
	if (offset.y < mTableView->minContainerOffset().y)
	{
		offset.y = mTableView->minContainerOffset().y;
	}
	//
	//mTableView->setContentOffsetInDuration(ccp(offset.x,offset.y),0.1f);
	mTableView->setContentOffset(ccp(offset.x,offset.y));
}


//CCLog("MainLayerZhuangBeiBaseBody::scrollToIndex --> index = %d",index);

//float min_offset = mTableView->minContainerOffset().y;		// -x
//float max_offset = mTableView->maxContainerOffset().y;		// 0
//float offset_y = -min_offset;

//for (int i=0;i<index;i++)
//{
//	offset_y += tableCellSizeForIndex(mTableView,i).height;
//}
//mTableView->setContentOffsetInDuration(ccp(mTableView->getContentOffset().x,offset_y),10.1f);

}*

setContentOffsetInDuration,或者setContentOffset

同问,我对这个控件表示很无语,那个函数一直不知道有啥用

得到那行的偏移,再setcontentoffset就行了!

static void scrollToIndex(CCTableView * m_table, int index){
auto cellHeight = m_table->getContentOffset().y;

m_table->setContentOffset(ccp(0,
                              cellHeight
                              + m_table->getDataSource()->tableCellSizeForIndex(m_table, 0).height
                              * 10));

}

listView->scrollCellToBack(20, true);

void scrollToIndex(int index)
{
// index不对不操作
if (index < 0 || index > numberOfCellsInTableView(mTableView) - 1)
return;

{
	// 保存所有的位置数据
	std::vector<float> m_vCellsPositions;
	{
		int cellNum = numberOfCellsInTableView(mTableView);
		 m_vCellsPositions.resize(cellNum + 1, 0.0);

		if (cellNum > 0)
		{
			float currentPos = 0;
			CCSize cellSize;
			for (int i=0; i < cellNum; i++)
			{
				m_vCellsPositions* = currentPos;
				cellSize = tableCellSizeForIndex(mTableView, i);
				switch (mTableView->getDirection())
				{
				case kCCScrollViewDirectionHorizontal:
					currentPos += cellSize.width;
					break;
				default:
					currentPos += cellSize.height;
					break;
				}
			}
			m_vCellsPositions = currentPos;//1 extra value allows us to get right/bottom of the last cell
		}
	}
	

	//  __offsetFromIndex
	CCPoint offset;
	{
		//CCSize  cellSize;
		switch (mTableView->getDirection())
		{
		case kCCScrollViewDirectionHorizontal:
			offset = ccp(m_vCellsPositions, 0.0f);
			break;
		default:
			offset = ccp(0.0f, m_vCellsPositions);
			break;
		}
	}
	


	// _offsetFromIndex
	const CCSize curr_cellSize = tableCellSizeForIndex(mTableView, index);
	if (mTableView->getVerticalFillOrder() == kCCTableViewFillTopDown)
	{
		offset.y = mTableView->getContainer()->getContentSize().height - offset.y - curr_cellSize.height;
	}

	// 移动container,把cell放在最上面
	offset.y = -offset.y;		// cell在y处,那么container就去要移动-y才可以看得到
	// 设置位置,此时offset是刚好在最下面,需要移动到最上面
	offset.y = offset.y + mTableView->getViewSize().height - curr_cellSize.height;

	if (offset.y > mTableView->maxContainerOffset().y)
	{
		offset.y = mTableView->maxContainerOffset().y;
	}
	if (offset.y < mTableView->minContainerOffset().y)
	{
		offset.y = mTableView->minContainerOffset().y;
	}
	//
	//mTableView->setContentOffsetInDuration(ccp(offset.x,offset.y),0.1f);
	mTableView->setContentOffset(ccp(offset.x,offset.y));
}


//CCLog("MainLayerZhuangBeiBaseBody::scrollToIndex --> index = %d",index);

//float min_offset = mTableView->minContainerOffset().y;		// -x
//float max_offset = mTableView->maxContainerOffset().y;		// 0
//float offset_y = -min_offset;

//for (int i=0;i<index;i++)
//{
//	offset_y += tableCellSizeForIndex(mTableView,i).height;
//}
//mTableView->setContentOffsetInDuration(ccp(mTableView->getContentOffset().x,offset_y),10.1f);

}*