CAListView(列表)

2018-08-29 18:36 更新

类说明

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的代码实现。

#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 属性介绍

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)

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号