课程表

入门与环境搭建

简单控件的使用

视图控制

调用、存储、网络和动画

其他控件的使用

工具箱
速查手册

CAListView(列表)

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

类说明

CAListView和CAScrollView非常相似,只是其内部成列表状,支持水平方案和竖直方向的滑动。常用于一些列表信息的展示,如:通讯录、新闻列表、目录索引等。


CAListView使用起来相对比较复杂,一般我们要同时使用CAListView、CAListViewCell、CAListViewDelegate、CAListViewDataSource来同时构建我们的列表界面,这么我们先分别了解一下它们的作用:

CAListView就是列表控件,是显示列表的载体,它是由多个CAListViewCell列组成的。

CAListViewCell是组成列表的每一个单元,下面我们都简称为cell

CAListViewDelegate是CAListView的交互代理,主要代理选择cell和取消选择cell的事件

CAListViewDataSource是CAListView的数据代理,主要代理cell的数量、cell的高度和将cell添加到CAListView显示。


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

属性说明
ListViewOrientationlistView的滚动方向
ListViewDataSource添加数据代理
ListViewDelegate添加交互代理
ListHeaderView添加头部视图
ListFooterView添加尾部视图
SeparatorColor设置cell分割线的颜色
ListHeaderHeight设置头部视图的高度
ListFooterHeight设置尾部视图的高度
SeparatorViewHeight设置cell分割线的高度
AllowsHeadAndFootHover允许头和尾的悬停
AllowsSelection是否开启cell选择
AllowsMultipleSelection是否可以多选cell


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

方法说明
setAllowsSelection是否开启cell选择
setAllowsMultipleSelection是否可以多选cell
setSelectAtIndex根据索引设置cell为选中状态
setUnSelectAtIndex根据索引设置cell为未选中状态
setShowsScrollIndicators设置显示滚动条
dequeueReusableCellWithIdentifier可以重用单元标示符
cellForRowAtIndex通过cell索引获取Index
switchPCMode开关PC模式
ccTouchBegan触摸事件开始时的回调函数
ccTouchMoved触摸事件中触点移动时的回调函数
ccTouchEnded触摸事件结束时的回调函数
ccTouchCancelled触摸非正常结束时的回调函数。(例如:电话或锁屏)
mouseMoved鼠标移动
mouseMovedOutSide鼠标移出


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

方法说明
listViewDidSelectCellAtIndex选中cell时调用
listViewDidDeselectCellAtIndex取消选择cell时调用


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

方法说明
numberOfIndexcell的总数量
listViewHeightForIndexcell的高度
listViewCellAtIndex添加生成cell
listViewWillDisplayCellAtIndex回调当前将要显示的CAListView


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

属性说明
ContentView获得内容视图
BackgroundView设置背景视图
ReuseIdentifier设置重用标识符
Index获得重用标识符
ControlStateEffect设置控制状态效应
AllowsSelectedCAListViewCell是否可以选择


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

方法说明
create创建,默认Frame为(0,0,0,0)
initWithReuseIdentifier重用标识符初始化


了解CAListView的主要函数,我们来实现一个CAListView的列表视图。

第一步:创建我们自己的cell

我们需要创建一个先的class,我这里创建一个MyCell,并继承CAListViewCell。用于每个列表单元的布局显示,下面看一下MyCell.h和MyCell.cpp的代码实现。

  1. #pragma once
  2. #include "CrossApp.h"
  3. class MyCell : public CAListViewCell
  4. {
  5. public:
  6. MyCell();
  7. ~MyCell();
  8. //创建MyCell
  9. static MyCell* create(const std::string& identifier, const DRect& _rect = DRectZero);
  10. public:
  11. //初始化Cell
  12. void initWithCell();
  13. //设置回调
  14. void cellBtnCallback(CAControl* btn, DPoint point);
  15. protected:
  16. //正常状态
  17. virtual void normalListViewCell();
  18. //高亮状态
  19. virtual void highlightedListViewCell();
  20. //选中状态
  21. virtual void selectedListViewCell();
  22. //禁用状态
  23. virtual void disabledListViewCell();
  24. };

MyCell.cpp代码如下:

  1. #include "MyCell.h"
  2. MyCell::MyCell()
  3. {
  4. }
  5. MyCell::~MyCell()
  6. {
  7. }
  8. MyCell* MyCell::create(const std::string& identifier, const DRect& _rect)
  9. {
  10. MyCell* listViewCell = new MyCell();
  11. //设置重用标示符
  12. if (listViewCell&&listViewCell->initWithReuseIdentifier(identifier))
  13. {
  14. //设置Frame范围
  15. listViewCell->setFrame(_rect);
  16. //设置为内存自动释放
  17. listViewCell->autorelease();
  18. return listViewCell;
  19. }
  20. //如果创建失败安全释放内存
  21. CC_SAFE_DELETE(listViewCell);
  22. return NULL;
  23. }
  24. void MyCell::initWithCell()
  25. {
  26. //获得当前的宽度
  27. DSize _size = this->getFrame().size;
  28. //创建CALabel
  29. CALabel* test = CALabel::createWithCenter(DRect(_size.width*0.5,
  30. _size.height*0.5,
  31. _size.width*0.8,
  32. _size.height));
  33. test->setTextAlignment(CATextAlignmentCenter);
  34. test->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
  35. test->setFontSize(_px(40));
  36. test->setTag(100);
  37. this->addSubview(test);
  38. }
  39. void MyCell::cellBtnCallback(CAControl* btn, DPoint point)
  40. {
  41. CCLog("MyCell::cellBtnCallback-->");
  42. }
  43. void MyCell::normalListViewCell()
  44. {
  45. this->setBackgroundView(CAView::createWithColor(CAColor_white));
  46. }
  47. void MyCell::highlightedListViewCell()
  48. {
  49. this->setBackgroundView(CAView::createWithColor(CAColor_yellow));
  50. }
  51. void MyCell::selectedListViewCell()
  52. {
  53. this->setBackgroundView(CAView::createWithColor(CAColor_orange));
  54. }
  55. void MyCell::disabledListViewCell()
  56. {
  57. this->setBackgroundView(CAView::createWithColor(CAColor_black));
  58. }


CAListView 属性介绍

ListViewOrientation 

类型:CAListViewOrientation*

解释:listView的滚动方向。set/get{}。


ListViewDataSource

类型:CAListViewDataSource*

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


ListViewDelegate    

类型:CAListViewDelegate*

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


ListHeaderView      

类型:CAView*

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


ListFooterView      

类型:CAView*

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


SeparatorColor     

类型:CAColor4B

解释:设置cell分割线的颜色。set/get{}。


ListHeaderHeight

类型:unsigned int

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


ListFooterHeight

类型:unsigned int

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


SeparatorViewHeight

类型:unsigned int

解释:设置fell分割线的高度。set/get{}。


AllowsHeadAndFootHover  

类型:bool

解释:允许头和尾的悬停。set/get{}。


AllowsSelection   

类型:bool

解释:是否开启cell选择。is{}。


AllowsMultipleSelection    

类型:bool

解释:是否可以多选cell。is{}。


CAListView 方法介绍

virtual void setAllowsSelection(bool var);  

返回值:virtual void

参数:

类型
参数名
说明
boolvar 

解释:是否可以多选cell


void setSelectAtIndex(unsigned int index);  

返回值:void

参数:

类型
参数名
说明
unsigned intindexcell的索引值

解释:根据索引设置cell为选中状态


void setUnSelectAtIndex(unsigned int index);  

返回值:void

参数:

类型
参数名
说明
unsigned intindexcell的索引值

解释:根据索引设置cell为未选中状态


virtual void setShowsScrollIndicators(bool var); 

返回值:virtual void

参数:

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

解释:设置显示滚动条


CAListViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);

返回值:CAListViewCell*

参数:

类型参数名说明
const char*reuseIdentifier标识符

解释:可以重用单元标示符


CAListViewCell* cellForRowAtIndex(unsigned int index);

返回值:CAListViewCell*

参数:

类型参数名说明
unsigned intindexcell的索引值

解释:通过cell索引获取Index


virtual void switchPCMode(bool var)

返回值:virtual void

参数:

类型参数名说明
boolvar开关

解释:开关PC模式


virtual bool ccTouchBegan(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual bool

参数:

类型参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEvent此参数待定

解释:触摸事件开始时的回调函数


virtual void ccTouchMoved(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEvent此参数待定

解释:触摸事件中触点移动时的回调函数


virtual void ccTouchEnded(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEvent此参数待定

解释:触摸事件结束时的回调函数


virtual void ccTouchCancelled(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEvent此参数待定

解释:触摸非正常结束时的回调函数。(例如:电话或锁屏)


virtual void mouseMoved(CATouch* pTouch, CAEvent* pEvent);

返回值:virtual void

参数:

类型参数名说明
CATouch*pTouch传递对象
CAEvent*pEvent此参数待定

解释:鼠标移动


virtual void mouseMovedOutSide(CATouch* pTouch, CAEvent* pEvent);

返回值:virtual void

参数:

类型参数名说明
CATouch*pTouch传递对象
CAEvent*pEvent此参数待定

解释:鼠标移出


CAListViewDelegate 方法介绍

virtual void listViewDidSelectCellAtIndex(CAListView *listView, unsigned int index)        

返回值:virtual void

参数:

类型参数名说明
CAListViewlistView当前的listView
unsigned intindexcell的索引值

解释:选中cell时调用

        

virtual void listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index)        

返回值:virtual void

参数:

类型参数名说明
CAListViewlistView当前的listView
unsigned intindexcell的索引值

解释:取消选择cell时调用


CAListViewDataSource方法介绍

virtual unsigned int numberOfIndex(CAListView *listView)        

返回值:virtual unsigned int

参数:

类型参数名说明
CAListViewlistView当前的listView

解释:cell的总数量


virtual unsigned int listViewHeightForIndex(CAListView *listView, unsigned int index)        

返回值:virtual unsigned int

参数:

类型参数名说明
CAListViewlistView当前的listView
unsigned intindexcell的索引值

解释:cell的高度


virtual CAListViewCell* listViewCellAtIndex(CAListView *listView, const DSize& cellSize, unsigned int index)       

返回值:virtual CAListViewCell*

参数:

类型参数名说明
CAListViewlistView当前的listView
DSizecellSizecell的size
unsigned intindexcell的索引值

解释:添加生成cell


virtual void listViewWillDisplayCellAtIndex(CAListView* table, CAListViewCell* cell, unsigned int index) ;

返回值:virtual void

参数:

类型参数名说明
CAListViewlistView当前的listView
CAListViewCellcell显示添加的cell
unsigned intindexcell的索引值

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


CAListViewCell 属性介绍

ContentView

类型:CAView*

解释:获得内容视图。get{}。


BackgroundView

类型:CAView*

解释:设置背景视图。set/get{}。


ReuseIdentifier

类型:std::string

解释:设置重用标识符。set/get{}。


Index

类型:unsigned int

解释:获得重用标识符。set/get{}。


ControlStateEffect

类型:bool

解释:设置控制状态效应。is/set{}。


AllowsSelected

类型:bool

解释:CAListViewCell是否可以选择。is/set{}。


CAListViewCell 方法介绍

static CAListViewCell* create(const std::string& reuseIdentifier);

返回值:static CAListViewCell*

参数:

类型参数名说明
std::string&reuseIdentifier重用标识符

解释:创建,默认Frame为(0,0,0,0)


      

virtual bool initWithReuseIdentifier(const std::string& reuseIdentifier);

返回值:virtual bool

参数:

类型参数名说明
std::string&reuseIdentifier重用标识符

解释:创建一个空CAListViewCell,默认Frame为(0,0,0,0)

转载本站内容时,请务必注明来自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号