类说明
CAListView和CAScrollView非常相似,只是其内部成列表状,支持水平方案和竖直方向的滑动。常用于一些列表信息的展示,如:通讯录、新闻列表、目录索引等。
CAListView使用起来相对比较复杂,一般我们要同时使用CAListView、CAListViewCell、CAListViewDelegate、CAListViewDataSource来同时构建我们的列表界面,这么我们先分别了解一下它们的作用:
CAListView就是列表控件,是显示列表的载体,它是由多个CAListViewCell列组成的。
CAListViewCell是组成列表的每一个单元,下面我们都简称为cell
CAListViewDelegate是CAListView的交互代理,主要代理选择cell和取消选择cell的事件
CAListViewDataSource是CAListView的数据代理,主要代理cell的数量、cell的高度和将cell添加到CAListView显示。
CAListView 属性(点击查看方法介绍)
| 属性 | 说明 | 
| ListViewOrientation | listView的滚动方向 | 
| 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 方法(点击查看方法介绍)
| 方法 | 说明 | 
| numberOfIndex | cell的总数量 | 
| listViewHeightForIndex | cell的高度 | 
| listViewCellAtIndex | 添加生成cell | 
| listViewWillDisplayCellAtIndex | 回调当前将要显示的CAListView | 
CAListViewCell 属性(点击查看方法介绍)
| 属性 | 说明 | 
| ContentView | 获得内容视图 | 
| BackgroundView | 设置背景视图 | 
| ReuseIdentifier | 设置重用标识符 | 
| Index | 获得重用标识符 | 
| ControlStateEffect | 设置控制状态效应 | 
| AllowsSelected | CAListViewCell是否可以选择 | 
CAListViewCell 方法(点击查看方法介绍)
| 方法 | 说明 | 
| create | 创建,默认Frame为(0,0,0,0) | 
| initWithReuseIdentifier | 重用标识符初始化 | 
了解CAListView的主要函数,我们来实现一个CAListView的列表视图。
第一步:创建我们自己的cell
我们需要创建一个先的class,我这里创建一个MyCell,并继承CAListViewCell。用于每个列表单元的布局显示,下面看一下MyCell.h和MyCell.cpp的代码实现。
#pragma once
#include "CrossApp.h"
class MyCell : public CAListViewCell
{
public:
    MyCell();
    ~MyCell();
     
    //创建MyCell
    static MyCell* create(const std::string& identifier, const DRect& _rect = DRectZero);
     
public:
    //初始化Cell
    void initWithCell();
    
    //设置回调
    void cellBtnCallback(CAControl* btn, DPoint point);
     
protected:
    //正常状态
    virtual void normalListViewCell();
    
    //高亮状态
    virtual void highlightedListViewCell();
    
    //选中状态
    virtual void selectedListViewCell();
    
    //禁用状态
    virtual void disabledListViewCell();   
};
MyCell.cpp代码如下:
#include "MyCell.h"
 
MyCell::MyCell()
{
}
 
MyCell::~MyCell()
{
}
 
MyCell* MyCell::create(const std::string& identifier, const DRect& _rect)
{
    MyCell* listViewCell = new MyCell();
     
    //设置重用标示符
    if (listViewCell&&listViewCell->initWithReuseIdentifier(identifier))
    {
        //设置Frame范围
        listViewCell->setFrame(_rect);
        
        //设置为内存自动释放
        listViewCell->autorelease();
        return listViewCell;
    }
    
    //如果创建失败安全释放内存
    CC_SAFE_DELETE(listViewCell);
    return NULL;
}
 
void MyCell::initWithCell()
{
    //获得当前的宽度
    DSize _size = this->getFrame().size;
     
    //创建CALabel
    CALabel* test = CALabel::createWithCenter(DRect(_size.width*0.5,
        _size.height*0.5,
        _size.width*0.8,
        _size.height));
    test->setTextAlignment(CATextAlignmentCenter);
    test->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
    test->setFontSize(_px(40));
    test->setTag(100);
    this->addSubview(test);
}
 
void MyCell::cellBtnCallback(CAControl* btn, DPoint point)
{
    CCLog("MyCell::cellBtnCallback-->");
}
 
void MyCell::normalListViewCell()
{
    this->setBackgroundView(CAView::createWithColor(CAColor_white));
}
 
void MyCell::highlightedListViewCell()
{
    this->setBackgroundView(CAView::createWithColor(CAColor_yellow));
}
 
void MyCell::selectedListViewCell()
{
    this->setBackgroundView(CAView::createWithColor(CAColor_orange));
}
 
void MyCell::disabledListViewCell()
{
    this->setBackgroundView(CAView::createWithColor(CAColor_black));
}
CAListView 属性介绍
类型:CAListViewOrientation*
解释:listView的滚动方向。set/get{}。
类型:CAListViewDataSource*
解释:添加数据代理。set/get{}。
类型:CAListViewDelegate*
解释:添加交互代理。set/get{}。
类型:CAView*
解释:添加头部视图。set/get{}。
类型:CAView*
解释:添加尾部视图。set/get{}。
类型:CAColor4B
解释:设置cell分割线的颜色。set/get{}。
类型:unsigned int
解释:设置头部视图的高度。set/get{}。
类型:unsigned int
解释:设置尾部视图的高度。set/get{}。
类型:unsigned int
解释:设置fell分割线的高度。set/get{}。
类型:bool
解释:允许头和尾的悬停。set/get{}。
类型:bool
解释:是否开启cell选择。is{}。
类型:bool
解释:是否可以多选cell。is{}。
CAListView 方法介绍
virtual void setAllowsSelection(bool var);
返回值:virtual void
参数:
| 类型 | 参数名 | 说明 | 
| bool | var | 
解释:是否可以多选cell
void setSelectAtIndex(unsigned int index);
返回值:void
参数:
| 类型 | 参数名 | 说明 | 
| unsigned int | index | cell的索引值 | 
解释:根据索引设置cell为选中状态
void setUnSelectAtIndex(unsigned int index);
返回值:void
参数:
| 类型 | 参数名 | 说明 | 
| unsigned int | index | cell的索引值 | 
解释:根据索引设置cell为未选中状态
virtual void setShowsScrollIndicators(bool var);
返回值:virtual void
参数:
| 类型 | 参数名 | 说明 | 
| bool | var | 是否显示滚动条 | 
解释:设置显示滚动条
CAListViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);
返回值:CAListViewCell*
参数:
| 类型 | 参数名 | 说明 | 
| const char* | reuseIdentifier | 标识符 | 
解释:可以重用单元标示符
CAListViewCell* cellForRowAtIndex(unsigned int index);
返回值:CAListViewCell*
参数:
| 类型 | 参数名 | 说明 | 
| unsigned int | index | cell的索引值 | 
解释:通过cell索引获取Index
virtual void switchPCMode(bool var)
返回值:virtual void
参数:
| 类型 | 参数名 | 说明 | 
| bool | var | 开关 | 
解释:开关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
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
| unsigned int | index | cell的索引值 | 
解释:选中cell时调用
virtual void listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index)
返回值:virtual void
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
| unsigned int | index | cell的索引值 | 
解释:取消选择cell时调用
CAListViewDataSource方法介绍
virtual unsigned int numberOfIndex(CAListView *listView)
返回值:virtual unsigned int
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
解释:cell的总数量
virtual unsigned int listViewHeightForIndex(CAListView *listView, unsigned int index)
返回值:virtual unsigned int
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
| unsigned int | index | cell的索引值 | 
解释:cell的高度
virtual CAListViewCell* listViewCellAtIndex(CAListView *listView, const DSize& cellSize, unsigned int index)
返回值:virtual CAListViewCell*
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
| DSize | cellSize | cell的size | 
| unsigned int | index | cell的索引值 | 
解释:添加生成cell
virtual void listViewWillDisplayCellAtIndex(CAListView* table, CAListViewCell* cell, unsigned int index) ;
返回值:virtual void
参数:
| 类型 | 参数名 | 说明 | 
| CAListView | listView | 当前的listView | 
| CAListViewCell | cell | 显示添加的cell | 
| unsigned int | index | cell的索引值 | 
解释:回调当前将要显示的CAListView
CAListViewCell 属性介绍
类型:CAView*
解释:获得内容视图。get{}。
类型:CAView*
解释:设置背景视图。set/get{}。
类型:std::string
解释:设置重用标识符。set/get{}。
类型:unsigned int
解释:获得重用标识符。set/get{}。
类型:bool
解释:设置控制状态效应。is/set{}。
类型: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)