示例#1
0
void CACollectionView::loadCollectionCell()
{
	CCRect rect = this->getBounds();
	rect.origin = getContentOffset();
	rect.origin.y -= rect.size.height * 0.1f;
	rect.size.height *= 1.2f;

	std::map<CAIndexPath3E, CACollectionViewCell*>::iterator itr;
	for (itr = m_pUsedCollectionCells.begin(); itr != m_pUsedCollectionCells.end(); itr++)
	{
		CC_CONTINUE_IF(itr->second != NULL);

		CAIndexPath3E r = itr->first;
		CCRect cellRect = m_rUsedCollectionCellRects[r];
		CC_CONTINUE_IF(!rect.intersectsRect(cellRect));

		CACollectionViewCell* cell = m_pCollectionViewDataSource->collectionCellAtIndex(this, cellRect.size, r.section, r.row, r.item);
		if (cell)
		{
			cell->m_nSection = r.section;
			cell->m_nRow = r.row;
			cell->m_nItem = r.item;
			cell->updateDisplayedAlpha(this->getAlpha());
			this->addSubview(cell);
			cell->setFrame(cellRect);
			itr->second = cell;

			if (m_pSelectedCollectionCells.count(r))
			{
				cell->setControlStateSelected();
			}
		}
	}
}
void CACollectionView::reloadData()
{
	CC_RETURN_IF(m_pCollectionViewDataSource == NULL);
    
	this->reloadViewSizeData();
    
	this->removeAllSubviews();
    
	DRect winRect = this->getBounds();
	winRect.origin = getContentOffset();
	float width = winRect.size.width;
	int y = 0;
    
	if (m_nCollectionHeaderHeight > 0 && m_pCollectionHeaderView)
	{
		m_pCollectionHeaderView->setDisplayRange(true);
		m_pCollectionHeaderView->setFrame(DRect(0, y, width, m_nCollectionHeaderHeight));
		addSubview(m_pCollectionHeaderView);
		y += m_nCollectionHeaderHeight;
	}
    
;
    int begin = (int)m_rSectionRects.size();
    m_rSectionRects.resize(m_rSectionRects.size() + m_nSections);
	for (int i = 0; i < m_nSections; i++)
	{
		unsigned int iSectionHeaderHeight = m_nSectionHeaderHeights.at(i);
		DRect sectionHeaderRect = DRect(0, y, width, iSectionHeaderHeight);
		if (iSectionHeaderHeight>0)
		{
			CAView* pSectionHeaderView = m_pCollectionViewDataSource->collectionViewSectionViewForHeaderInSection(this, sectionHeaderRect.size, i);
			if (pSectionHeaderView != NULL)
			{
				pSectionHeaderView->setDisplayRange(true);
				pSectionHeaderView->setFrame(sectionHeaderRect);
				insertSubview(pSectionHeaderView, 1);
				m_pSectionHeaderViews[i] = pSectionHeaderView;
				y += iSectionHeaderHeight;
			}
		}
        
		y += m_nVertInterval;
		unsigned int rowCount = m_nRowsInSections.at(i);
		for (int j = 0; j < rowCount; j++)
		{
			int iHeight = m_nRowHeightss.at(i).at(j);
            
			unsigned int itemCount = m_pCollectionViewDataSource->numberOfItemsInRowsInSection(this, i, j);
            
			unsigned int cellWidth = 0;
			if (itemCount>0)
			{
				cellWidth = (width - m_nHoriInterval) / itemCount - m_nHoriInterval;
			}
			for (int k = 0; k < itemCount; k++)
			{
				CAIndexPath3E indexPath = CAIndexPath3E(i, j, k);
				DRect cellRect = DRect(m_nHoriInterval + (cellWidth + m_nHoriInterval)*k, y, cellWidth, iHeight);
				m_rUsedCollectionCellRects[indexPath] = cellRect;
                
				std::pair<std::map<CAIndexPath3E, CACollectionViewCell*>::iterator, bool> itrResult =
                m_mpUsedCollectionCells.insert(std::make_pair(indexPath, (CACollectionViewCell*)NULL));
                
				CC_CONTINUE_IF(!winRect.intersectsRect(cellRect));
                
				CACollectionViewCell* cell = m_pCollectionViewDataSource->collectionCellAtIndex(this, cellRect.size, i, j, k);
				if (cell)
				{
					addSubview(cell);
					cell->setFrame(cellRect);
					cell->m_nSection = i;
					cell->m_nRow = j;
					cell->m_nItem = k;
					itrResult.first->second = cell;
                    m_vpUsedCollectionCells.pushBack(cell);
                    
                    if (m_pCollectionViewDataSource)
                    {
                        m_pCollectionViewDataSource->collectionViewWillDisplayCellAtIndex(this, cell, i, j, k);
                    }
				}
			}
			y += (iHeight + m_nVertInterval);
		}
        
		unsigned int iSectionFooterHeight = m_nSectionFooterHeights.at(i);
		DRect sectionFooterRect = DRect(0, y, width, iSectionFooterHeight);
		if (iSectionFooterHeight > 0)
		{
			CAView* pSectionFooterView = m_pCollectionViewDataSource->collectionViewSectionViewForFooterInSection(this, sectionFooterRect.size, i);
			if (pSectionFooterView != NULL)
			{
				pSectionFooterView->setDisplayRange(true);
				pSectionFooterView->setFrame(sectionFooterRect);
				insertSubview(pSectionFooterView, 1);
				m_pSectionFooterViews[i] = pSectionFooterView;
				y += iSectionFooterHeight;
			}
		}
        
		DRect sectionRect = sectionHeaderRect;
		sectionRect.size.height = sectionFooterRect.origin.y
        + sectionFooterRect.size.height
        - sectionHeaderRect.origin.y;
        m_rSectionRects[begin + i] = sectionRect;
	}
    
	if (m_nCollectionFooterHeight > 0 && m_pCollectionFooterView)
	{
		m_pCollectionFooterView->setFrame(DRect(0, y, width, m_nCollectionFooterHeight));
		addSubview(m_pCollectionFooterView);
		y += m_nCollectionFooterHeight;
	}
    
	this->updateSectionHeaderAndFooterRects();
	this->layoutPullToRefreshView();
	this->startDeaccelerateScroll();
}