关于CCMultiColumnTableView一些小问题的修改

由于项目需要,需要一行显示多个cell 所以在github上找了一个CCMulticolumnTableView

但是!有一个小BUG

如果 direction == kCCScrollViewDirectionHorizontal && cellSize.width != cellSize.height 

cellpos会有小问题


所以 上代码!

/****************************************************************************
Copyright (c) 2012 @hermanjakobi
Copyright (c) 2010 Sangwoo Im


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#ifndef __CCMULTITABLEVIEWCELL_H__
#define __CCMULTITABLEVIEWCELL_H__

#include "CCTableView.h"


NS_CC_EXT_BEGIN

	/**
	* It adds multiple column support to CCTableView.
	* Use CCScrollViewDirectionBoth to set custom column count, colCount.
	* If a specific direction is set, table automatically find a proper value,
	* computing from viewSize and cellSize.
	*/

class CCMultiColumnTableView : public CCTableView 
{
public:
	CCMultiColumnTableView();

	/**
	* An intialized table view object
	*
	* @param dataSource data source
	* @param size view size
	* @return table view
	*/
	static CCMultiColumnTableView* create(CCTableViewDataSource* dataSource, CCSize size);
	/**
	* An initialized table view object
	*
	* @param dataSource data source;
	* @param size view size
	* @param container parent object for cells
	* @return table view
	*/
	static CCMultiColumnTableView* create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container);

	/**
	* the maximum number of columns.
	*/
	unsigned int m_colCount;
    
    virtual void reloadData();

	void setColCount(unsigned int cols);

    CC_SYNTHESIZE(int, headSize, HeadSize);
protected:

	virtual int __indexFromOffset(CCPoint offset);
	virtual CCPoint __offsetFromIndex(unsigned int index);
	virtual void _updateContentSize();
	virtual void _updateCellPositions();
private:

};

NS_CC_EXT_END

#endif /* __CCMULTITABLEVIEWCELL_H__ */


/****************************************************************************
Copyright (c) 2012 @hermanjakobi
Copyright (c) 2010 Sangwoo Im


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "cocos2d.h"
#include "CCMultiColumnTableView.h"
#include "CCTableViewCell.h"

NS_CC_EXT_BEGIN

CCMultiColumnTableView* CCMultiColumnTableView::create(CCTableViewDataSource* dataSource, CCSize size)
{
	return CCMultiColumnTableView::create(dataSource, size, NULL);
}

CCMultiColumnTableView* CCMultiColumnTableView::create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container)
{
	CCMultiColumnTableView *table = new CCMultiColumnTableView();
	table->initWithViewSize(size, container);
	table->autorelease();
	table->setDataSource(dataSource);
	table->_updateCellPositions();
	table->_updateContentSize();

	return table;
}

CCMultiColumnTableView::CCMultiColumnTableView() : m_colCount(1) , headSize(0)
{
}

void CCMultiColumnTableView::setColCount(unsigned int cols)
{
	m_colCount = cols;

	if (this->getDirection() == kCCScrollViewDirectionBoth) 
	{
		this->_updateCellPositions();
	}

	this->_updateContentSize();
}

void CCMultiColumnTableView::reloadData(void)
{
    CCTableView::reloadData();
    
    if (this->getDirection() == kCCScrollViewDirectionBoth)
    {
        this->_updateCellPositions();
    }
    
    this->_updateContentSize();
}

int CCMultiColumnTableView::__indexFromOffset(CCPoint offset)
{
	int  index = 0;
	CCSize  cellSize;
	int  col, row;
	float    spaceWidth;

	cellSize = m_pDataSource->cellSizeForTable(this);

	switch (this->getDirection()) {
	case kCCScrollViewDirectionHorizontal:
		spaceWidth = this->getContainer()->getContentSize().height / m_colCount;
		col        = (offset.y - (spaceWidth - cellSize.height)*0.5)/spaceWidth;
		row        = offset.x / cellSize.width;    
		break;
	default:
		spaceWidth = this->getContainer()->getContentSize().width / m_colCount;
		col        = (offset.x - (spaceWidth - cellSize.width)*0.5)/spaceWidth;
		row        = (offset.y - headSize) / cellSize.height;
		break;
	}

	index = col + row * m_colCount;
	return index;
}

CCPoint CCMultiColumnTableView::__offsetFromIndex(unsigned int index)
{
	CCPoint offset;
	CCSize  cellSize;
	float    spaceWidth;
	int  col, row;

	cellSize = m_pDataSource->cellSizeForTable(this);

	switch (this->getDirection()) 
	{
	case kCCScrollViewDirectionHorizontal:
		row        = index / m_colCount;
		col        = index % m_colCount;
		spaceWidth = this->getContainer()->getContentSize().height / m_colCount;
		offset     = ccp(row * cellSize.width, col * spaceWidth + (spaceWidth - cellSize.height) * 0.5);
		break;

	default:
		row        = index / m_colCount;
		col        = index % m_colCount;
		spaceWidth = this->getContainer()->getContentSize().width / m_colCount;
		offset     = ccp(col * spaceWidth + (spaceWidth - cellSize.width) * 0.5, headSize + row * cellSize.height);
		break;
	}

	return offset;

}

void CCMultiColumnTableView::_updateCellPositions() 
{
    int cellsCount = m_pDataSource->numberOfCellsInTableView(this) / m_colCount;
    m_vCellsPositions.resize(cellsCount + 1, 0.0);

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

}

void CCMultiColumnTableView::_updateContentSize()
{
	CCSize     size, cellSize, viewSize;
	unsigned int cellCount, rows;

	cellSize  = m_pDataSource->cellSizeForTable(this);
	cellCount = m_pDataSource->numberOfCellsInTableView(this);
	viewSize  = CCSizeMake(getViewSize().width/getContainer()->getScaleX(), getViewSize().height/getContainer()->getScaleY());

	switch (this->getDirection())
	{
	case kCCScrollViewDirectionHorizontal:
		m_colCount = getViewSize().height / cellSize.height;
		rows     = ceilf(cellCount/((float)m_colCount));
		size     = CCSizeMake(MAX(rows * cellSize.width, viewSize.width), m_colCount * cellSize.height);
		break;

	default:
		if (getDirection() == kCCScrollViewDirectionVertical) {
			m_colCount = viewSize.width / cellSize.width;   
		}
		rows     = ceilf(cellCount/((float)m_colCount));
		size     = CCSizeMake(MAX(cellSize.width * m_colCount, viewSize.width), headSize + rows * cellSize.height);
		break;
	}

	this->setContentSize(size);

	//FIXME? unsure if that code makes sense
	if (m_eOldDirection != m_eDirection)
	{
		if (m_eDirection == kCCScrollViewDirectionHorizontal)
		{
			this->setContentOffset(ccp(0,0));
		}
		else
		{
			//this->setContentOffset(ccp(0,0));
			this->setContentOffset(ccp(0,this->minContainerOffset().y));
		}

		m_eOldDirection = m_eDirection;
	}
}

NS_CC_EXT_END



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值