CAListView(列表)

2018-08-29 18:36 更新

類說明

CAListView和CAScrollView非常相似,只是其內部成列表狀,支持水平方案和豎直方向的滑動。常用于一些列表信息的展示,如:通訊錄、新聞列表、目錄索引等。


CAListView使用起來相對比較復雜,一般我們要同時使用CAListView、CAListViewCell、CAListViewDelegate、CAListViewDataSource來同時構建我們的列表界面,這么我們先分別了解一下它們的作用:

CAListView就是列表控件,是顯示列表的載體,它是由多個CAListViewCell列組成的。

CAListViewCell是組成列表的每一個單元,下面我們都簡稱為cell

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

CAListViewDataSource是CAListView的數(shù)據(jù)代理,主要代理cell的數(shù)量、cell的高度和將cell添加到CAListView顯示。


CAListView 屬性(點擊查看方法介紹)

屬性說明
ListViewOrientationlistView的滾動方向
ListViewDataSource添加數(shù)據(jù)代理
ListViewDelegate添加交互代理
ListHeaderView添加頭部視圖
ListFooterView添加尾部視圖
SeparatorColor設置cell分割線的顏色
ListHeaderHeight設置頭部視圖的高度
ListFooterHeight設置尾部視圖的高度
SeparatorViewHeight設置cell分割線的高度
AllowsHeadAndFootHover允許頭和尾的懸停
AllowsSelection是否開啟cell選擇
AllowsMultipleSelection是否可以多選cell


CAListView 方法(點擊查看方法介紹)

方法說明
setAllowsSelection是否開啟cell選擇
setAllowsMultipleSelection是否可以多選cell
setSelectAtIndex根據(jù)索引設置cell為選中狀態(tài)
setUnSelectAtIndex根據(jù)索引設置cell為未選中狀態(tài)
setShowsScrollIndicators設置顯示滾動條
dequeueReusableCellWithIdentifier可以重用單元標示符
cellForRowAtIndex通過cell索引獲取Index
switchPCMode開關PC模式
ccTouchBegan觸摸事件開始時的回調函數(shù)
ccTouchMoved觸摸事件中觸點移動時的回調函數(shù)
ccTouchEnded觸摸事件結束時的回調函數(shù)
ccTouchCancelled觸摸非正常結束時的回調函數(shù)。(例如:電話或鎖屏)
mouseMoved鼠標移動
mouseMovedOutSide鼠標移出


CAListViewDelegate 方法(點擊查看方法介紹)

方法說明
listViewDidSelectCellAtIndex選中cell時調用
listViewDidDeselectCellAtIndex取消選擇cell時調用


CAListViewDataSource 方法(點擊查看方法介紹)

方法說明
numberOfIndexcell的總數(shù)量
listViewHeightForIndexcell的高度
listViewCellAtIndex添加生成cell
listViewWillDisplayCellAtIndex回調當前將要顯示的CAListView


CAListViewCell 屬性(點擊查看方法介紹)

屬性說明
ContentView獲得內容視圖
BackgroundView設置背景視圖
ReuseIdentifier設置重用標識符
Index獲得重用標識符
ControlStateEffect設置控制狀態(tài)效應
AllowsSelectedCAListViewCell是否可以選擇


CAListViewCell 方法(點擊查看方法介紹)

方法說明
create創(chuàng)建,默認Frame為(0,0,0,0)
initWithReuseIdentifier重用標識符初始化


了解CAListView的主要函數(shù),我們來實現(xiàn)一個CAListView的列表視圖。

第一步:創(chuàng)建我們自己的cell

我們需要創(chuàng)建一個先的class,我這里創(chuàng)建一個MyCell,并繼承CAListViewCell。用于每個列表單元的布局顯示,下面看一下MyCell.h和MyCell.cpp的代碼實現(xiàn)。

#pragma once
#include "CrossApp.h"
class MyCell : public CAListViewCell
{
public:
    MyCell();
    ~MyCell();
     
    //創(chuàng)建MyCell
    static MyCell* create(const std::string& identifier, const DRect& _rect = DRectZero);
     
public:
    //初始化Cell
    void initWithCell();
    
    //設置回調
    void cellBtnCallback(CAControl* btn, DPoint point);
     
protected:
    //正常狀態(tài)
    virtual void normalListViewCell();
    
    //高亮狀態(tài)
    virtual void highlightedListViewCell();
    
    //選中狀態(tài)
    virtual void selectedListViewCell();
    
    //禁用狀態(tài)
    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;
    }
    
    //如果創(chuàng)建失敗安全釋放內存
    CC_SAFE_DELETE(listViewCell);
    return NULL;
}
 
void MyCell::initWithCell()
{
    //獲得當前的寬度
    DSize _size = this->getFrame().size;
     
    //創(chuàng)建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*

解釋:添加數(shù)據(jù)代理。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

參數(shù):

類型
參數(shù)名
說明
boolvar 

解釋:是否可以多選cell


void setSelectAtIndex(unsigned int index);  

返回值:void

參數(shù):

類型
參數(shù)名
說明
unsigned intindexcell的索引值

解釋:根據(jù)索引設置cell為選中狀態(tài)


void setUnSelectAtIndex(unsigned int index);  

返回值:void

參數(shù):

類型
參數(shù)名
說明
unsigned intindexcell的索引值

解釋:根據(jù)索引設置cell為未選中狀態(tài)


virtual void setShowsScrollIndicators(bool var); 

返回值:virtual void

參數(shù):

類型
參數(shù)名
說明
boolvar是否顯示滾動條

解釋:設置顯示滾動條


CAListViewCell* dequeueReusableCellWithIdentifier(const char* reuseIdentifier);

返回值:CAListViewCell*

參數(shù):

類型參數(shù)名說明
const char*reuseIdentifier標識符

解釋:可以重用單元標示符


CAListViewCell* cellForRowAtIndex(unsigned int index);

返回值:CAListViewCell*

參數(shù):

類型參數(shù)名說明
unsigned intindexcell的索引值

解釋:通過cell索引獲取Index


virtual void switchPCMode(bool var)

返回值:virtual void

參數(shù):

類型參數(shù)名說明
boolvar開關

解釋:開關PC模式


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

返回值:virtual bool

參數(shù):

類型參數(shù)名說明
CATouch*pTouch觸摸傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:觸摸事件開始時的回調函數(shù)


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CATouch*pTouch觸摸傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:觸摸事件中觸點移動時的回調函數(shù)


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CATouch*pTouch觸摸傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:觸摸事件結束時的回調函數(shù)


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CATouch*pTouch觸摸傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:觸摸非正常結束時的回調函數(shù)。(例如:電話或鎖屏)


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CATouch*pTouch傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:鼠標移動


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CATouch*pTouch傳遞對象
CAEvent*pEvent此參數(shù)待定

解釋:鼠標移出


CAListViewDelegate 方法介紹

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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CAListViewlistView當前的listView
unsigned intindexcell的索引值

解釋:選中cell時調用

        

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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
CAListViewlistView當前的listView
unsigned intindexcell的索引值

解釋:取消選擇cell時調用


CAListViewDataSource方法介紹

virtual unsigned int numberOfIndex(CAListView *listView)        

返回值:virtual unsigned int

參數(shù):

類型參數(shù)名說明
CAListViewlistView當前的listView

解釋:cell的總數(shù)量


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

返回值:virtual unsigned int

參數(shù):

類型參數(shù)名說明
CAListViewlistView當前的listView
unsigned intindexcell的索引值

解釋:cell的高度


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

返回值:virtual CAListViewCell*

參數(shù):

類型參數(shù)名說明
CAListViewlistView當前的listView
DSizecellSizecell的size
unsigned intindexcell的索引值

解釋:添加生成cell


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

返回值:virtual void

參數(shù):

類型參數(shù)名說明
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

解釋:設置控制狀態(tài)效應。is/set{}。


AllowsSelected

類型:bool

解釋:CAListViewCell是否可以選擇。is/set{}。


CAListViewCell 方法介紹

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

返回值:static CAListViewCell*

參數(shù):

類型參數(shù)名說明
std::string&reuseIdentifier重用標識符

解釋:創(chuàng)建,默認Frame為(0,0,0,0)


      

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

返回值:virtual bool

參數(shù):

類型參數(shù)名說明
std::string&reuseIdentifier重用標識符

解釋:創(chuàng)建一個空CAListViewCell,默認Frame為(0,0,0,0)

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號