课程表

入门与环境搭建

简单控件的使用

视图控制

调用、存储、网络和动画

其他控件的使用

工具箱
速查手册

CACollectionView(容器)

当前位置:免费教程 » 移动开发 » CrossApp

类说明

CACollectionView同CATableView类似,主要用于数据的展示,实现了tableView的基本功能,同时对tableView拓展,更完美的进行展示数据。


CACollectionView的使用方法和CATableView比较类似,我们也要分别使用:CACollectionView、CACollectionViewCell、CACollectionViewDelegate、CACollectionViewDataSource来构建。

CACollectionView是表格视图的容器,是容器的载体。

CACollectionViewCell是表格视图的一个单元(本节后面简称cell)。

CACollectionViewDelegate是交互代理,响应cell选中和取消状态。

CACollectionViewDataSource是数据代理,设置Selection个数及Selection包含cell个数。


CACollectionView 属性(点击查看方法介绍)

属性说明
CollectionViewDataSource添加数据代理
CollectionViewDelegate添加交互代理
CollectionHeaderView添加头部视图
CollectionFooterView添加尾部视图
CollectionHeaderHeight设置头部的高度
CollectionFooterHeight设置尾部的高度
HoriInterval水平间隔
VertInterval垂直间隔
AllowsSelection允许选择
AllowsMultipleSelection允许多个选择
AlwaysTopSectionHeader总是顶部的标题
AlwaysBottomSectionFooter总是底部的节尾


CACollectionView 方法(点击查看方法介绍)

方法说明
createWithFrame创建,并指定其Frame,默认Frame为(0,0,0,0)
createWithCenter创建,并指定其Center,默认Center为(0,0,0,0)
dequeueReusableCellWithIdentifier从复用队列中寻找指定标识符的cell
setAllowsSelection是否开启cell选择
setAllowsMultipleSelection是否可以多选cell
setSelectRowAtIndexPath通过索引选择一行
setUnSelectRowAtIndexPath通过索引取消选择一行
setShowsScrollIndicators设置显示滚动指示器
cellForRowAtIndexPath根据索引获取显示的cell
getHighlightCollectionCell获取高亮显示的collectioncell
switchPCMode开关PC模式
init初始化
clearData清除数据
reloadData重载数据


CACollectionViewDelegate 方法(点击查看方法介绍)

方法说明
collectionViewDidSelectCellAtIndexPath选中cell时调用
collectionViewDidDeselectCellAtIndexPath取消选择cell时调用


CACollectionViewDataSource 方法(点击查看方法介绍)

方法说明
collectionCellAtIndex获取指定cell
collectionViewHeightForRowAtIndexPathcell的高度
numberOfItemsInRowsInSection每个cell里的item数量
numberOfRowsInSection获取对应的section所包含的cell个数
numberOfSections获取tableview包含的section个数
collectionViewSectionViewForHeaderInSectionheaderView的内容
collectionViewHeightForHeaderInSection每个section的headerView
collectionViewSectionViewForFooterInSectionfooterView的内容
collectionViewHeightForFooterInSection每个section的footerView
collectionViewWillDisplayCellAtIndex回调当前将要显示的collectionView


我们本机的示例,不再使用自定义的CACollectionViewCell的方法来实现,我们来看看本节的示例代码:

FirstViewController.h内容:

  1. #ifndef __HelloCpp__ViewController__
  2. #define __HelloCpp__ViewController__
  3. #include <iostream>
  4. #include "CrossApp.h"
  5. USING_NS_CC;
  6. class FirstViewController : public CAViewController, CACollectionViewDelegate, CACollectionViewDataSource
  7. {
  8. public:
  9. FirstViewController();
  10. virtual ~FirstViewController();
  11. protected:
  12. void viewDidLoad();
  13. void viewDidUnload();
  14. public:
  15. //选中item时调用
  16. virtual void collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item);
  17. //取消item是调用
  18. virtual void collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item);
  19. //获取指定cell
  20. virtual CACollectionViewCell* collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item);
  21. //section的个数
  22. virtual unsigned int numberOfSections(CACollectionView *collectionView);
  23. //section中的cell个数
  24. virtual unsigned int numberOfRowsInSection(CACollectionView *collectionView, unsigned int section);
  25. //每个cell中Item的个数
  26. virtual unsigned int numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row);
  27. //cell的高度
  28. virtual unsigned int collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row);
  29. private:
  30. //用于获得屏幕的size
  31. CADipSize size;
  32. //CACollectionView
  33. CACollectionView* p_Conllection;
  34. //颜色容器
  35. std::vector<CAColor4B> colorArr;
  36. };
  37. #endif /* defined(__HelloCpp__ViewController__) */

FirstViewController.cpp内容:

  1. #include "FirstViewController.h"
  2. FirstViewController::FirstViewController()
  3. {
  4. }
  5. FirstViewController::~FirstViewController()
  6. {
  7. }
  8. void FirstViewController::viewDidLoad()
  9. {
  10. //获得屏幕大小
  11. size = this->getView()->getBounds().size;
  12. //随机出颜色
  13. for (int i = 0; i < 40; i++)
  14. {
  15. char r = CCRANDOM_0_1() * 255;
  16. char g = CCRANDOM_0_1() * 255;
  17. char b = CCRANDOM_0_1() * 255;
  18. //将随机的ccc4对象放入到容器里
  19. colorArr.push_back(ccc4(r, g, b, 255));
  20. }
  21. //生成CACollectionView
  22. p_Conllection = CACollectionView::createWithFrame(this->getView()->getBounds());
  23. //开启选中
  24. p_Conllection->setAllowsSelection(true);
  25. //开启多选
  26. p_Conllection->setAllowsMultipleSelection(true);
  27. //绑定交互代理
  28. p_Conllection->setCollectionViewDelegate(this);
  29. //绑定数据代理
  30. p_Conllection->setCollectionViewDataSource(this);
  31. //item水平间的距离
  32. p_Conllection->setHoriInterval(40);
  33. //itme竖直间的距离
  34. p_Conllection->setVertInterval(40);
  35. //添加到屏幕渲染
  36. this->getView()->addSubview(p_Conllection);
  37. }
  38. void FirstViewController::viewDidUnload()
  39. {
  40. // Release any retained subviews of the main view.
  41. // e.g. self.myOutlet = nil;
  42. }
  43. void FirstViewController::collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item)
  44. {
  45. //选中
  46. CCLog("选中");
  47. }
  48. void FirstViewController::collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item)
  49. {
  50. //取消选中
  51. CCLog("取消选中");
  52. }
  53. CACollectionViewCell* FirstViewController::collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item)
  54. {
  55. //计算 如果cell个数大于颜色数组,则返回空
  56. if (row * 3 + item >= colorArr.size())
  57. {
  58. return NULL;
  59. }
  60. //获得
  61. DSize _size = cellSize;
  62. //根据标识获得CACollectionViewCell
  63. CACollectionViewCell* p_Cell = collectionView->dequeueReusableCellWithIdentifier("CrossApp");
  64. //如果没有找到相应的CACollectionViewCell则新建一个
  65. if (p_Cell == NULL)
  66. {
  67. p_Cell = CACollectionViewCell::create("CrossApp");
  68. //生成Item背景
  69. CAView* itemImage = CAView::createWithFrame(DRect(0, 0, _size.width, _size.height));
  70. itemImage->setTag(99);
  71. p_Cell->addSubview(itemImage);
  72. DSize itemSize = itemImage->getBounds().size;
  73. //生成itemCALabel
  74. CALabel* itemText = CALabel::createWithCenter(DRect(itemSize.width*0.5, itemSize.height*0.5, 150, 40));
  75. itemText->setTag(100);
  76. itemText->setFontSize(29);
  77. itemText->setTextAlignment(CATextAlignmentCenter);
  78. itemText->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
  79. itemImage->addSubview(itemText);
  80. }
  81. //设置Item背景颜色
  82. CAView* itemImageView = p_Cell->getSubviewByTag(99);
  83. itemImageView->setColor(colorArr.at(row * 3 + item));
  84. CCLog("%d", row * 3 + item);
  85. //设置itme文本显示
  86. char pos[20] = "";
  87. sprintf(pos, "(%d,%d,%d)", section, row, item);
  88. CALabel* itemText = (CALabel*)p_Cell->getSubviewByTag(99)->getSubviewByTag(100);
  89. itemText->setText(pos);
  90. return p_Cell;
  91. }
  92. unsigned int FirstViewController::numberOfSections(CACollectionView *collectionView)
  93. {
  94. return 1;
  95. }
  96. unsigned int FirstViewController::numberOfRowsInSection(CACollectionView *collectionView, unsigned int section)
  97. {
  98. return colorArr.size() % 3 == 0 ? colorArr.size() / 3 : colorArr.size() / 3 + 1;
  99. }
  100. unsigned int FirstViewController::numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row)
  101. {
  102. return 3;
  103. }
  104. unsigned int FirstViewController::collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row)
  105. {
  106. return (this->getView()->getBounds().size.width - 40 * 4) / 3;
  107. }

CACollectionView 属性说明

CollectionViewDataSource

类型:CACollectionViewDataSource*

解释:添加数据代理。set/get{}。

    

CollectionViewDelegate

类型:CACollectionViewDelegate*

解释:添加交互代理。set/get{}。


CollectionHeaderView

类型:CAView*

解释:添加头部视图。set/get{}。


CollectionFooterView

类型:CAView**

解释:添加尾部视图。set/get{}。


CollectionHeaderHeight

类型:unsigned int

解释:设置头部的高度set/get{}。


CollectionFooterHeight

类型:unsigned int

解释:设置尾部的高度set/get{}。

    

HoriIntervalsetHoriInterval

类型:unsigned int

解释:水平间隔。set/get{}。


VertInterval

类型:unsigned int

解释:垂直间隔。set/get{}。


AllowsSelection

类型:bool

解释:允许选择。is{}。


AllowsMultipleSelection

类型:bool

解释:允许多个选择。is{}。


AlwaysTopSectionHeader

类型:bool

解释:总是顶部的标题。is/set{}。


AlwaysBottomSectionFooter

类型:bool

解释:总是底部的节尾。is/set{}。


CACollectionView 方法说明

static CACollectionView* createWithFrame(const DRect& rect);

返回值:static CACollectionView*

参数:

类型参数名说明
DRectrect区域大小

解释:创建,并指定其Frame,默认Frame为(0,0,0,0)


static CACollectionView* createWithCenter(const DRect& rect);

返回值:static CACollectionView*

参数:

类型参数名说明
DRectrect中心点的位置及大小

解释:创建,并指定其Center,默认Center为(0,0,0,0)


CACollectionViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);

返回值:CACollectionViewCell*

参数:

类型参数名说明
charreuseIdentifier重用标识符

解释:从复用队列中寻找指定标识符的cell


virtual void setAllowsSelection(bool var);

返回值:virtual void

参数:

类型参数名说明
boolvar是否开启cell选择

解释:设置是否开启cell选择


virtual void setAllowsMultipleSelection(bool var);

返回值:virtual void

参数:

类型参数名说明
boolvar是否可以多选cell

解释:设置是否可以多选cell


void setSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);

返回值:void

参数:

类型参数名说明
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:设置索引路径选择行


void setUnSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);

返回值:void

参数:

类型参数名说明
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:设置索引路径不允许选择行


virtual void setShowsScrollIndicators(bool var);

返回值:virtual void

参数:

类型参数名说明
boolvar是否显示滚动指示器

解释:设置显示滚动指示器


CACollectionViewCell* cellForRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item);

返回值:CACollectionViewCell*

参数:

类型参数名说明
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:根据索引获取显示的cell


CACollectionViewCell* getHighlightCollectionCell();

返回值:CACollectionViewCell*

参数:

解释:获取高亮显示的collectioncell


virtual void switchPCMode(bool var);

返回值:virtual void

参数:

类型参数名说明
boolvar开关

解释:开关PC模式


virtual bool init();

返回值:virtual bool

参数:

解释:初始化


void clearData();

返回值:void

参数:

解释:清除数据


void reloadData();

返回值:void

参数:

解释:重载数据


CACollectionViewDelegate 方法说明

virtual void collectionViewDidSelectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item){};

返回值:virtual void

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:选中cell时调用


virtual void collectionViewDidDeselectCellAtIndexPath(CACollectionView *collectionView, unsigned int section, unsigned int row, unsigned int item){};

返回值:virtual void

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:取消选择cell时调用


CACollectionViewDataSource 方法说明

virtual CACollectionViewCell* collectionCellAtIndex(CACollectionView *collectionView, const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item)

返回值:virtual CACollectionViewCell*

参数:

类型参数名说明
CACollectionViewcollectionViewcell
DSizecellSizecell大小
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:获取指定cell


virtual unsigned int collectionViewHeightForRowAtIndexPath(CACollectionView* collectionView, unsigned int section, unsigned int row)

返回值:virtual unsigned int

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:cell的高度


virtual unsigned int numberOfItemsInRowsInSection(CACollectionView *collectionView, unsigned int section, unsigned int row)

返回值:virtual unsigned int

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:每个cell里的item数量


virtual unsigned int numberOfRowsInSection(CACollectionView *collectionView, unsigned int section)

返回值:virtual unsigned int

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection

解释:获取对应的section所包含的cell个数


virtual unsigned int numberOfSections(CACollectionView *collectionView)

返回值:virtual unsigned int

参数:

类型参数名说明
CACollectionViewcollectionViewcell

解释:获取tableview包含的section个数


virtual CAView* collectionViewSectionViewForHeaderInSection(CACollectionView *collectionView, const DSize& viewSize, unsigned int section)

返回值:virtual CAView*

参数:

类型参数名说明
CACollectionViewcollectionViewcell
DSizecellSizecell大小
unsigned intsectionSection

解释:headerView的内容


virtual unsigned int collectionViewHeightForHeaderInSection(CACollectionView *collectionView, unsigned int section)

返回值:virtual unsigned int 

参数:

类型参数名说明
CACollectionViewcollectionViewcell
unsigned intsectionSection

解释:每个section的headerView


virtual CAView* collectionViewSectionViewForFooterInSection(CACollectionView *collectionView, const DSize& viewSize, unsigned int section)

返回值:virtual CAView*

参数:

类型参数名说明
CACollectionViewcollectionViewcell
const DSize&viewSize视图大小
unsigned intsectionSection

解释:footerView的内容


virtual unsigned int collectionViewHeightForFooterInSection(CACollectionView *collectionView, unsigned int section)

返回值:virtual unsigned int

参数:

类型参数名说明
CACollectionViewcollectionViewcell
CCSizecellSizecell大小
unsigned intsectionSection

解释:每个section的footerView


virtual void collectionViewWillDisplayCellAtIndex(CACollectionView* table, CACollectionViewCell* cell, unsigned int section, unsigned int row, unsigned int item) {};

返回值:virtual void

参数:

类型参数名说明
CACollectionView*table
CACollectionViewcollectionViewcell
unsigned intsectionSection
unsigned int row
unsigned intitem项目

解释:回调当前将要显示的collectionView

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号