類說(shuō)明
CACollectionView同CATableView類似,主要用于數(shù)據(jù)的展示,實(shí)現(xiàn)了tableView的基本功能,同時(shí)對(duì)tableView拓展,更完美的進(jìn)行展示數(shù)據(jù)。
CACollectionView的使用方法和CATableView比較類似,我們也要分別使用:CACollectionView、CACollectionViewCell、CACollectionViewDelegate、CACollectionViewDataSource來(lái)構(gòu)建。
CACollectionView是表格視圖的容器,是容器的載體。
CACollectionViewCell是表格視圖的一個(gè)單元(本節(jié)后面簡(jiǎn)稱cell)。
CACollectionViewDelegate是交互代理,響應(yīng)cell選中和取消狀態(tài)。
CACollectionViewDataSource是數(shù)據(jù)代理,設(shè)置Selection個(gè)數(shù)及Selection包含cell個(gè)數(shù)。
CACollectionView 屬性(點(diǎn)擊查看方法介紹)
屬性 | 說(shuō)明 |
CollectionViewDataSource | 添加數(shù)據(jù)代理 |
CollectionViewDelegate | 添加交互代理 |
CollectionHeaderView | 添加頭部視圖 |
CollectionFooterView | 添加尾部視圖 |
CollectionHeaderHeight | 設(shè)置頭部的高度 |
CollectionFooterHeight | 設(shè)置尾部的高度 |
HoriInterval | 水平間隔 |
VertInterval | 垂直間隔 |
AllowsSelection | 允許選擇 |
AllowsMultipleSelection | 允許多個(gè)選擇 |
AlwaysTopSectionHeader | 總是頂部的標(biāo)題 |
AlwaysBottomSectionFooter | 總是底部的節(jié)尾 |
CACollectionView 方法(點(diǎn)擊查看方法介紹)
方法 | 說(shuō)明 |
createWithFrame | 創(chuàng)建,并指定其Frame,默認(rèn)Frame為(0,0,0,0) |
createWithCenter | 創(chuàng)建,并指定其Center,默認(rèn)Center為(0,0,0,0) |
dequeueReusableCellWithIdentifier | 從復(fù)用隊(duì)列中尋找指定標(biāo)識(shí)符的cell |
setAllowsSelection | 是否開(kāi)啟cell選擇 |
setAllowsMultipleSelection | 是否可以多選cell |
setSelectRowAtIndexPath | 通過(guò)索引選擇一行 |
setUnSelectRowAtIndexPath | 通過(guò)索引取消選擇一行 |
setShowsScrollIndicators | 設(shè)置顯示滾動(dòng)指示器 |
cellForRowAtIndexPath | 根據(jù)索引獲取顯示的cell |
getHighlightCollectionCell | 獲取高亮顯示的collectioncell |
switchPCMode | 開(kāi)關(guān)PC模式 |
init | 初始化 |
clearData | 清除數(shù)據(jù) |
reloadData | 重載數(shù)據(jù) |
CACollectionViewDelegate 方法(點(diǎn)擊查看方法介紹)
方法 | 說(shuō)明 |
collectionViewDidSelectCellAtIndexPath | 選中cell時(shí)調(diào)用 |
collectionViewDidDeselectCellAtIndexPath | 取消選擇cell時(shí)調(diào)用 |
CACollectionViewDataSource 方法(點(diǎn)擊查看方法介紹)
方法 | 說(shuō)明 |
collectionCellAtIndex | 獲取指定cell |
collectionViewHeightForRowAtIndexPath | cell的高度 |
numberOfItemsInRowsInSection | 每個(gè)cell里的item數(shù)量 |
numberOfRowsInSection | 獲取對(duì)應(yīng)的section所包含的cell個(gè)數(shù) |
numberOfSections | 獲取tableview包含的section個(gè)數(shù) |
collectionViewSectionViewForHeaderInSection | headerView的內(nèi)容 |
collectionViewHeightForHeaderInSection | 每個(gè)section的headerView |
collectionViewSectionViewForFooterInSection | footerView的內(nèi)容 |
collectionViewHeightForFooterInSection | 每個(gè)section的footerView |
collectionViewWillDisplayCellAtIndex | 回調(diào)當(dāng)前將要顯示的collectionView |
我們本機(jī)的示例,不再使用自定義的CACollectionViewCell的方法來(lái)實(shí)現(xiàn),我們來(lái)看看本節(jié)的示例代碼:
FirstViewController.h內(nèi)容:
#ifndef __HelloCpp__ViewController__
#define __HelloCpp__ViewController__
#include <iostream>
#include "CrossApp.h"
USING_NS_CC;
class FirstViewController : public CAViewController, CACollectionViewDelegate, CACollectionViewDataSource
{
public:
FirstViewController();
virtual ~FirstViewController();
protected:
void viewDidLoad();
void viewDidUnload();
public:
//選中item時(shí)調(diào)用
virtual void collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item);
//取消item是調(diào)用
virtual void collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item);
//獲取指定cell
virtual CACollectionViewCell* collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item);
//section的個(gè)數(shù)
virtual unsigned int numberOfSections(CACollectionView *collectionView);
//section中的cell個(gè)數(shù)
virtual unsigned int numberOfRowsInSection(CACollectionView *collectionView, unsigned int section);
//每個(gè)cell中Item的個(gè)數(shù)
virtual unsigned int numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row);
//cell的高度
virtual unsigned int collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row);
private:
//用于獲得屏幕的size
CADipSize size;
//CACollectionView
CACollectionView* p_Conllection;
//顏色容器
std::vector<CAColor4B> colorArr;
};
#endif /* defined(__HelloCpp__ViewController__) */
FirstViewController.cpp內(nèi)容:
#include "FirstViewController.h"
FirstViewController::FirstViewController()
{
}
FirstViewController::~FirstViewController()
{
}
void FirstViewController::viewDidLoad()
{
//獲得屏幕大小
size = this->getView()->getBounds().size;
//隨機(jī)出顏色
for (int i = 0; i < 40; i++)
{
char r = CCRANDOM_0_1() * 255;
char g = CCRANDOM_0_1() * 255;
char b = CCRANDOM_0_1() * 255;
//將隨機(jī)的ccc4對(duì)象放入到容器里
colorArr.push_back(ccc4(r, g, b, 255));
}
//生成CACollectionView
p_Conllection = CACollectionView::createWithFrame(this->getView()->getBounds());
//開(kāi)啟選中
p_Conllection->setAllowsSelection(true);
//開(kāi)啟多選
p_Conllection->setAllowsMultipleSelection(true);
//綁定交互代理
p_Conllection->setCollectionViewDelegate(this);
//綁定數(shù)據(jù)代理
p_Conllection->setCollectionViewDataSource(this);
//item水平間的距離
p_Conllection->setHoriInterval(40);
//itme豎直間的距離
p_Conllection->setVertInterval(40);
//添加到屏幕渲染
this->getView()->addSubview(p_Conllection);
}
void FirstViewController::viewDidUnload()
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
void FirstViewController::collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item)
{
//選中
CCLog("選中");
}
void FirstViewController::collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item)
{
//取消選中
CCLog("取消選中");
}
CACollectionViewCell* FirstViewController::collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item)
{
//計(jì)算 如果cell個(gè)數(shù)大于顏色數(shù)組,則返回空
if (row * 3 + item >= colorArr.size())
{
return NULL;
}
//獲得
DSize _size = cellSize;
//根據(jù)標(biāo)識(shí)獲得CACollectionViewCell
CACollectionViewCell* p_Cell = collectionView->dequeueReusableCellWithIdentifier("CrossApp");
//如果沒(méi)有找到相應(yīng)的CACollectionViewCell則新建一個(gè)
if (p_Cell == NULL)
{
p_Cell = CACollectionViewCell::create("CrossApp");
//生成Item背景
CAView* itemImage = CAView::createWithFrame(DRect(0, 0, _size.width, _size.height));
itemImage->setTag(99);
p_Cell->addSubview(itemImage);
DSize itemSize = itemImage->getBounds().size;
//生成itemCALabel
CALabel* itemText = CALabel::createWithCenter(DRect(itemSize.width*0.5, itemSize.height*0.5, 150, 40));
itemText->setTag(100);
itemText->setFontSize(29);
itemText->setTextAlignment(CATextAlignmentCenter);
itemText->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
itemImage->addSubview(itemText);
}
//設(shè)置Item背景顏色
CAView* itemImageView = p_Cell->getSubviewByTag(99);
itemImageView->setColor(colorArr.at(row * 3 + item));
CCLog("%d", row * 3 + item);
//設(shè)置itme文本顯示
char pos[20] = "";
sprintf(pos, "(%d,%d,%d)", section, row, item);
CALabel* itemText = (CALabel*)p_Cell->getSubviewByTag(99)->getSubviewByTag(100);
itemText->setText(pos);
return p_Cell;
}
unsigned int FirstViewController::numberOfSections(CACollectionView *collectionView)
{
return 1;
}
unsigned int FirstViewController::numberOfRowsInSection(CACollectionView *collectionView, unsigned int section)
{
return colorArr.size() % 3 == 0 ? colorArr.size() / 3 : colorArr.size() / 3 + 1;
}
unsigned int FirstViewController::numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row)
{
return 3;
}
unsigned int FirstViewController::collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row)
{
return (this->getView()->getBounds().size.width - 40 * 4) / 3;
}
CACollectionView 屬性說(shuō)明
類型:CACollectionViewDataSource*
解釋:添加數(shù)據(jù)代理。set/get{}。
類型:CACollectionViewDelegate*
解釋:添加交互代理。set/get{}。
類型:CAView*
解釋:添加頭部視圖。set/get{}。
類型:CAView**
解釋:添加尾部視圖。set/get{}。
類型:unsigned int
解釋:設(shè)置頭部的高度。set/get{}。
類型:unsigned int
解釋:設(shè)置尾部的高度。set/get{}。
類型:unsigned int
解釋:水平間隔。set/get{}。
類型:unsigned int
解釋:垂直間隔。set/get{}。
類型:bool
解釋:允許選擇。is{}。
類型:bool
解釋:允許多個(gè)選擇。is{}。
類型:bool
解釋:總是頂部的標(biāo)題。is/set{}。
類型:bool
解釋:總是底部的節(jié)尾。is/set{}。
CACollectionView 方法說(shuō)明
static CACollectionView* createWithFrame(const DRect& rect);
返回值:static CACollectionView*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
DRect | rect | 區(qū)域大小 |
解釋:創(chuàng)建,并指定其Frame,默認(rèn)Frame為(0,0,0,0)
static CACollectionView* createWithCenter(const DRect& rect);
返回值:static CACollectionView*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
DRect | rect | 中心點(diǎn)的位置及大小 |
解釋:創(chuàng)建,并指定其Center,默認(rèn)Center為(0,0,0,0)
CACollectionViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);
返回值:CACollectionViewCell*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
char | reuseIdentifier | 重用標(biāo)識(shí)符 |
解釋:從復(fù)用隊(duì)列中尋找指定標(biāo)識(shí)符的cell
virtual void setAllowsSelection(bool var);
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
bool | var | 是否開(kāi)啟cell選擇 |
解釋:設(shè)置是否開(kāi)啟cell選擇
virtual void setAllowsMultipleSelection(bool var);
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
bool | var | 是否可以多選cell |
解釋:設(shè)置是否可以多選cell
void setSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:設(shè)置索引路徑選擇行
void setUnSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:設(shè)置索引路徑不允許選擇行
virtual void setShowsScrollIndicators(bool var);
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
bool | var | 是否顯示滾動(dòng)指示器 |
解釋:設(shè)置顯示滾動(dòng)指示器
CACollectionViewCell* cellForRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);
返回值:CACollectionViewCell*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:根據(jù)索引獲取顯示的cell
CACollectionViewCell* getHighlightCollectionCell();
返回值:CACollectionViewCell*
參數(shù):
解釋:獲取高亮顯示的collectioncell
virtual void switchPCMode(bool var);
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
bool | var | 開(kāi)關(guān) |
解釋:開(kāi)關(guān)PC模式
返回值:virtual bool
參數(shù):
解釋:初始化
返回值:void
參數(shù):
解釋:清除數(shù)據(jù)
返回值:void
參數(shù):
解釋:重載數(shù)據(jù)
CACollectionViewDelegate 方法說(shuō)明
virtual void collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item){};
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:選中cell時(shí)調(diào)用
virtual void collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item){};
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:取消選擇cell時(shí)調(diào)用
CACollectionViewDataSource 方法說(shuō)明
virtual CACollectionViewCell* collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item)
返回值:virtual CACollectionViewCell*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
DSize | cellSize | cell大小 |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:獲取指定cell
virtual unsigned int collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:cell的高度
virtual unsigned int numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:每個(gè)cell里的item數(shù)量
virtual unsigned int numberOfRowsInSection(CACollectionView *collectionView, unsigned int section)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
解釋:獲取對(duì)應(yīng)的section所包含的cell個(gè)數(shù)
virtual unsigned int numberOfSections(CACollectionView *collectionView)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
解釋:獲取tableview包含的section個(gè)數(shù)
virtual CAView* collectionViewSectionViewForHeaderInSection(CACollectionView *collectionView, const DSize& viewSize, unsigned int section)
返回值:virtual CAView*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
DSize | cellSize | cell大小 |
unsigned int | section | Section |
解釋:headerView的內(nèi)容
virtual unsigned int collectionViewHeightForHeaderInSection(CACollectionView *collectionView, unsigned int section)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
解釋:每個(gè)section的headerView
virtual CAView* collectionViewSectionViewForFooterInSection(CACollectionView *collectionView, const DSize& viewSize, unsigned int section)
返回值:virtual CAView*
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
const DSize& | viewSize | 視圖大小 |
unsigned int | section | Section |
解釋:footerView的內(nèi)容
virtual unsigned int collectionViewHeightForFooterInSection(CACollectionView *collectionView, unsigned int section)
返回值:virtual unsigned int
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView | collectionView | cell |
CCSize | cellSize | cell大小 |
unsigned int | section | Section |
解釋:每個(gè)section的footerView
virtual void collectionViewWillDisplayCellAtIndex(CACollectionView* table, CACollectionViewCell* cell, unsigned int section, unsigned int row, unsigned int item) {};
返回值:virtual void
參數(shù):
類型 | 參數(shù)名 | 說(shuō)明 |
CACollectionView* | table | 表 |
CACollectionView | collectionView | cell |
unsigned int | section | Section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目 |
解釋:回調(diào)當(dāng)前將要顯示的collectionView
更多建議: