類說明:
CAAutoCollectionView同CATableView類似,主要用于數(shù)據(jù)的展示,實(shí)現(xiàn)了tableView的基本功能,同時(shí)對(duì)tableView拓展,更完美的進(jìn)行展示數(shù)據(jù)。
CAAutoCollectionView的使用方法和CATableView比較類似,我們也要分別使用:CAAutoCollectionView、CACollectionViewCell、CAAutoCollectionViewDelegate、CAAutoCollectionViewDataSource來構(gòu)建。
CAAutoCollectionView是表格視圖的容器,是容器的載體。
CACollectionViewCell是表格視圖的一個(gè)單元(本節(jié)后面簡(jiǎn)稱cell)。
CAAutoCollectionViewDelegate是交互代理,響應(yīng)cell選中和取消狀態(tài)。
CAAutoCollectionViewDataSource是數(shù)據(jù)代理,設(shè)置Selection個(gè)數(shù)及Selection包含Item個(gè)數(shù)。
CAAutoCollectionView 屬性(點(diǎn)擊查看方法介紹)
屬性 | 說明 |
CollectionViewDataSource | 添加數(shù)據(jù)代理 |
CollectionViewDelegate | 添加交互代理 |
CollectionHeaderView | 添加頭部視圖 |
CollectionFooterView | 添加尾部視圖 |
CollectionHeaderHeight | 設(shè)置頭部的高度 |
CollectionFooterHeight | 設(shè)置尾部的高度 |
CollectionViewOrientation | CollectionView方向取向 |
CollectionViewCellHoriAlign | CollectionView的Cell水平對(duì)齊 |
CollectionViewCellVertAlign | CollectionView的Cell垂直對(duì)齊 |
HoriCellInterval | cell水平間隔 |
VertCellInterval | cell垂直間隔 |
HoriMargins | 水平邊距 |
VertMargins | 垂直邊距 |
AllowsSelection | 允許選擇 |
AllowsMultipleSelection | 允許多個(gè)選擇 |
AlwaysTopSectionHeader | 總是頂部的標(biāo)題 |
AlwaysBottomSectionFooter | 總是底部的節(jié)尾 |
CAAutoCollectionView 方法(點(diǎn)擊查看方法介紹)
說明 | 說明 |
createWithFrame | 創(chuàng)建,并指定其Frame |
createWithCenter | 創(chuàng)建,并指定Color |
init | 初始化 |
reloadData | 重載數(shù)據(jù) |
dequeueReusableCellWithIdentifier | 從復(fù)用隊(duì)列中尋找指定標(biāo)識(shí)符的cell |
setAllowsSelection | 是否開啟cell選擇 |
setAllowsMultipleSelection | 是否可以多選cell |
setSelectRowAtIndexPath | 通過索引選擇一行 |
setUnSelectRowAtIndexPath | 通過索引取消選擇一行 |
setShowsScrollIndicators | 設(shè)置顯示滾動(dòng)指示器 |
cellForRowAtIndexPath | 根據(jù)索引獲取顯示的cell |
displayingCollectionCell | 顯示CollectionCell |
getHighlightCollectionCell | 獲取高亮顯示的collectioncell |
switchPCMode | 開關(guān)PC模式 |
我們本機(jī)的示例,不再使用自定義的CACollectionViewCell的方法來實(shí)現(xiàn),我們來看看本節(jié)的示例代碼:
FirstViewController.h內(nèi)容:
#ifndef __HelloCpp__ViewController__
#define __HelloCpp__ViewController__
#include <iostream>
#include "CrossApp.h"
USING_NS_CC;
class FirstViewController : public CAViewController, CAAutoCollectionViewDataSource, CAAutoCollectionViewDelegate
{
protected:
void viewDidLoad();
void viewDidUnload();
public:
FirstViewController();
virtual ~FirstViewController();
//選中
virtual void collectionViewDidSelectCellAtIndexPath(CAAutoCollectionView *collectionView, unsigned int section, unsigned int item);
//取消選中
virtual void collectionViewDidDeselectCellAtIndexPath(CAAutoCollectionView *collectionView, unsigned int section, unsigned int item);
//獲取指定cell
virtual CACollectionViewCell* collectionCellAtIndex(CAAutoCollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int item);
//項(xiàng)目大小
virtual DSize collectionViewSizeForItemAtIndexPath(CAAutoCollectionView* collectionView, unsigned int section, unsigned int item);
//每個(gè)Section中Item的個(gè)數(shù)
virtual unsigned int numberOfItemsInSection(CAAutoCollectionView *collectionView, unsigned int section);
//section的個(gè)數(shù)
virtual unsigned int numberOfSections(CAAutoCollectionView *collectionView);
private:
DSize size;
CAAutoCollectionView* p_AutoCollection;
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_AutoCollection = CAAutoCollectionView::createWithFrame(this->getView()->getBounds());
DRect rect = this->getView()->getBounds();
CCLog("MaxX = %f", rect.getMaxX());
CCLog("MaxX = %f", rect.getMaxY());
//開啟選中
p_AutoCollection->setAllowsSelection(true);
//開啟多選
p_AutoCollection->setAllowsMultipleSelection(true);
//綁定交互代理
p_AutoCollection->setCollectionViewDelegate(this);
//綁定數(shù)據(jù)代理
p_AutoCollection->setCollectionViewDataSource(this);
//item水平間的距離
p_AutoCollection->setHoriMargins(40);
p_AutoCollection->setHoriCellInterval(40);
//p_AutoCollection->setCollectionHeaderHeight(40);
//itme豎直間的距離
p_AutoCollection->setVertMargins(40);
p_AutoCollection->setVertCellInterval(40);
//p_AutoCollection->setCollectionFooterHeight(40);
//添加到屏幕渲染
this->getView()->addSubview(p_AutoCollection);
}
void FirstViewController::viewDidUnload()
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
//選中
void FirstViewController::collectionViewDidSelectCellAtIndexPath(CAAutoCollectionView *collectionView, unsigned int section, unsigned int item)
{
//選中
CCLog("選中");
}
//取消選中
void FirstViewController::collectionViewDidDeselectCellAtIndexPath(CAAutoCollectionView *collectionView, unsigned int section, unsigned int item)
{
//取消選中
CCLog("取消選中");
}
//獲取指定cell
CACollectionViewCell* FirstViewController::collectionCellAtIndex(CAAutoCollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int item)
{
//根據(jù)標(biāo)識(shí)獲得CACollectionViewCell
CACollectionViewCell* p_Cell = collectionView->dequeueReusableCellWithIdentifier("CrossApp");
//如果沒有找到相應(yīng)的CACollectionViewCell則新建一個(gè)
if (p_Cell == NULL)
{
p_Cell = CACollectionViewCell::create("CrossApp");
//生成Item背景
CAView* itemImage = CAView::createWithFrame(DRect(0, 0, cellSize.width, cellSize.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(item));
CCLog("row = %d", item);
//設(shè)置itme文本顯示
char pos[20] = "";
sprintf(pos, "(%d,%d)", section, item);
CALabel* itemText = (CALabel*)p_Cell->getSubviewByTag(99)->getSubviewByTag(100);
itemText->setText(pos);
return p_Cell;
}
//項(xiàng)目大小
DSize FirstViewController::collectionViewSizeForItemAtIndexPath(CAAutoCollectionView* collectionView, unsigned int section, unsigned int item)
{
DSize size;
size.width = (this->getView()->getBounds().size.width - 40 * 4) / 3;
size.height = (this->getView()->getBounds().size.width - 40 * 4) / 3;
return size;
}
//每個(gè)Section中Item的個(gè)數(shù)
unsigned int FirstViewController::numberOfItemsInSection(CAAutoCollectionView *collectionView, unsigned int section)
{
return 15;
}
//section的個(gè)數(shù)
unsigned int FirstViewController::numberOfSections(CAAutoCollectionView *collectionView)
{
return 10;
}
CAAutoCollectionView 屬性說明
類型:CAAutoCollectionViewDataSource*
解釋:添加數(shù)據(jù)代理,set/get{}。
類型:CAAutoCollectionViewDelegate*
解釋:添加交互代理,set/get{}。
類型:CAView*
解釋:添加頭部視圖,set/get{}。
類型:CAView*
解釋:添加尾部視圖,set/get{}。
類型:unsigned int
解釋:設(shè)置頭部的高度,set/get{}。
類型:unsigned int
解釋:設(shè)置尾部的高度,set/get{}。
類型:CACollectionViewOrientation
解釋:CollectionView方向取向,set/get{}。
類型:CACollectionViewCellHoriAlign
解釋:CollectionView的Cell水平對(duì)齊,set/get{}。
類型:CACollectionViewCellVertAlign
解釋:CollectionView的Cell垂直對(duì)齊,set/get{}。
類型:unsigned int
解釋:cell水平間隔,set/get{}。
類型:unsigned int
解釋:cell垂直間隔,set/get{}。
類型:unsigned int
解釋:水平邊距,set/get{}。
類型:unsigned int
解釋:垂直邊距,set/get{}。
類型:bool
解釋:允許選擇,is{}。
類型:bool
解釋:允許多個(gè)選擇,is{}。
類型:bool
解釋:總是頂部的標(biāo)題,is/set{}。
類型:bool
解釋:總是底部的節(jié)尾,is/set{}。
CAAutoCollectionView 方法說明
static CAAutoCollectionView* createWithFrame(const DRect& rect);
返回值:CAAutoCollectionView*
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 區(qū)域大小 |
解釋:創(chuàng)建,并指定其Frame
static CAAutoCollectionView* createWithCenter(const DRect& rect);
返回值:CAAutoCollectionView*
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 中心點(diǎn)的位置及大小 |
解釋:創(chuàng)建,并指定Color
返回值:bool
參數(shù):
解釋:初始化
返回值:void
參數(shù):
解釋:重載數(shù)據(jù)
CACollectionViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);
返回值:CACollectionViewCell*
參數(shù):
類型 | 參數(shù)名 | 說明 |
const char* | reuseIdentifier | 重載標(biāo)識(shí)符 |
解釋:從復(fù)用隊(duì)列中尋找指定標(biāo)識(shí)符的cell
virtual void setAllowsSelection(bool var);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
bool | var | 是否開啟 |
解釋:是否開啟cell選擇
virtual void setAllowsMultipleSelection(bool var);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
bool | var | 是否開啟 |
解釋:是否可以多選cell
void setSelectRowAtIndexPath(unsigned int section, unsigned int item);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
unsigned int | section | section |
unsigned int | item | 項(xiàng)目數(shù)量 |
解釋:通過索引選擇一行
void setUnSelectRowAtIndexPath(unsigned int section, unsigned int item);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
unsigned int | section | section |
unsigned int | item | 項(xiàng)目數(shù)量 |
解釋:通過索引取消選擇一行
virtual void setShowsScrollIndicators(bool var);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
bool | var | 是否開啟 |
解釋:設(shè)置顯示滾動(dòng)指示器
CACollectionViewCell* cellForRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);
返回值:CACollectionViewCell*
參數(shù):
類型 | 參數(shù)名 | 說明 |
unsigned int | section | section |
unsigned int | row | 行 |
unsigned int | item | 項(xiàng)目數(shù)量 |
解釋:根據(jù)索引獲取顯示的cell
const CAVector<CACollectionViewCell*>& displayingCollectionCell();
返回值:CAVector<CACollectionViewCell*>&
參數(shù):
解釋:顯示CollectionCell
CACollectionViewCell* getHighlightCollectionCell();
返回值:CACollectionViewCell*
參數(shù):
解釋:獲取高亮顯示的collectioncell
virtual void switchPCMode(bool var);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
bool | var | 是否開啟 |
解釋:開關(guān)PC模式
更多建議: