[寒江孤葉丶的CrossApp之旅_11][入門系列]通過Demo學習CrossApp之SecondViewController篇

來源:互聯網
上載者:User

標籤:crossapp   ios   android   tableview   

原創文章,歡迎轉載,轉載請註明:文章來自[寒江孤葉丶的CrossApp之旅系列]

部落格地址:http://blog.csdn.net/qq446569365

本文章是我在讀Demo時候隨手寫的注釋,分享出來供大家交流探討。如有不對之處歡迎指出!

SecondViewController.h

#ifndef _Second_ViewController_h_#define _Second_ViewController_h_#include <iostream>#include "CrossApp.h"#include "CrossAppExt.h"#include "Info.h"USING_NS_CC_EXT;using namespace CSJson;#define NUM 8class SecondViewController : public CAViewController, CATableViewDelegate, CATableViewDataSource,CAScrollViewDelegate{   public:    //解構函式SecondViewController();virtual ~SecondViewController();    protected:    //生命週期的回呼函數    void viewDidLoad();    void viewDidUnload();virtual void viewDidAppear();    //從json中讀取資料void loadJsonData(void);public:    //監聽根View(父節點view)的Size變化  如果當前viewController的根view的大小發生變化,則監聽此介面就可以擷取到變化後的size。virtual void reshapeViewRectDidFinish();        //表格項被選中時候產生的回調訊息,三個參數分別是  觸發事件的table,所點擊項目所在的section(可以理解為分區),所點擊項目在分區中的第幾行virtual void tableViewDidSelectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row);    //基本同上,只是被選中項目改為取消選中項virtual void tableViewDidDeselectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row);    //為tableView建立cell,採用複用的方式,當滾動table的時候,會回調這個函數,在這個函數中,設定table中項目的顯示資料,有點類似於Android的形式virtual CATableViewCell* tableCellAtIndex(CATableView* table, const CCSize& cellSize, unsigned int section, unsigned int row);    //該函數用於設定每一個Section的頭欄內容,三個參數 第一個是回調的是哪個table,第二個是頭欄的Size,第三個是哪個section的頭欄,    //這個函數有點類似於上邊的tableCellAtIndexvirtual CAView* tableViewSectionViewForHeaderInSection(CATableView* table, const CCSize& viewSize, unsigned int section);//與上邊含義相同只不過這個是尾欄的    virtual CAView* tableViewSectionViewForFooterInSection(CATableView* table, const CCSize& viewSize, unsigned int section);//根據Section的編號返回Section中有多少行   這裡返回多少行,繪製table時候就會繪製多少行    virtual unsigned int numberOfRowsInSection(CATableView *table, unsigned int section);    //table中有多少個Sectionvirtual unsigned int numberOfSections(CATableView *table);    //向table返回 row的高度,可以根據回調時傳入的row和section返回不同的值,來設定table中欄目的高度virtual unsigned int tableViewHeightForRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row);    //基本同上,這個是設定頭欄的高度virtual unsigned int tableViewHeightForHeaderInSection(CATableView* table, unsigned int section);    //基本同上,設定尾欄的高度virtual unsigned int tableViewHeightForFooterInSection(CATableView* table, unsigned int section);    //CAScrollViewDelegate的回呼函數,當使用者從上方下拉重新整理時候觸發virtual void scrollViewHeaderBeginRefreshing(CAScrollView* view);    //基本同上,不過是底部下拉重新整理時候觸發virtual void scrollViewFooterBeginRefreshing(CAScrollView* view);public:    //重新整理資料的函數 (用於 scrollViewFooterBeginRefreshing 和 scrollViewHeaderBeginRefreshing 觸發時候調用他重新整理列表) 使用者可以在開發時候,將其設定為從網路擷取最新資料後回調重新整理void refreshTableViewData(float interval);    // Section中有個按鈕,點一下展開,點一下關閉,這個是那個按鈕的點擊回呼函數void switchCellListInSection(CAControl* btn,CCPoint point);    //一個不明覺厲的函數,現在已經被廢棄了void closeCellListInSection(CAControl* btn, CCPoint point);private:CADipSize size;CATableView* p_TableView;    //sect是用於儲存每個section中有多少個row 嘛,就是多少行int sect[NUM];    //用於儲存資料的CADeque<Info*> personList;    //是否是頂部重新整理的flagbool isPullUpRefresh;};#endif 

SecondViewController.cpp

#include "SecondViewController.h"#define CAColor_blueStyle ccc4(51,204,255,255)#define CELL_COUNT 16//建構函式,將 isPullUpRefresh 設定為true  然後初始化sect中所有資料為CELL_COUNT(就是16),即:將所有section設定為展開狀態SecondViewController::SecondViewController() :isPullUpRefresh(true){for (int i = 0; i < NUM; i++){sect[i] = CELL_COUNT;}}//解構函式,清空personList資料SecondViewController::~SecondViewController(){personList.clear();}void SecondViewController::viewDidLoad(void){loadJsonData();//初始化顯示的資料,這裡是從Json讀取資料 以顯示在table中 建議跟入進去看看    //建立CAPullToRefreshView 拖動重新整理的View,CA真是方便啊……CAPullToRefreshView是view的顯示類型,同三種,頭、尾和自訂CAPullToRefreshView* headerRefreshView = CAPullToRefreshView::create(CAPullToRefreshView::CAPullToRefreshTypeHeader);CAPullToRefreshView* footerRefreshView = CAPullToRefreshView::create(CAPullToRefreshView::CAPullToRefreshTypeFooter);    //建立一個和view大小相同的TableViewp_TableView = CATableView::createWithFrame(this->getView()->getBounds());    //將資料類設定為this(因為本身這個類繼承自TableViewDataSource )p_TableView->setTableViewDataSource(this);    //將table的觸發事件託管類設定為thisp_TableView->setTableViewDelegate(this);    //設定為可選模式   還可以設定為多選模式 setAllowsMultipleSelectionp_TableView->setAllowsSelection(true);    //設定table的滾動view 的事件託管類(table的滾動顯示功能是因為table繼承自scrollview)p_TableView->setScrollViewDelegate(this);    //設定頭部向下拖動重新整理顯示的viewp_TableView->setHeaderRefreshView(headerRefreshView);    //設定尾部向下拖動重新整理顯示的viewp_TableView->setFooterRefreshView(footerRefreshView);this->getView()->addSubview(p_TableView);}void SecondViewController::viewDidAppear(){    //設定上邊的那條CANavigationBarItem* item = CANavigationBarItem::create("ViewController2");this->getTabBarController()->setNavigationBarItem(item);}void SecondViewController::loadJsonData(){Reader reader;Value value;string jsonFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("information.json");CCString *jsonData = CCString::createWithContentsOfFile(jsonFile.c_str());if (reader.parse(jsonData->getCString(), value)){int length = value["info"].size();CCLog("%d",length);for (int index = 0; index < length; index++){Info* personInfo = new Info();personInfo->autorelease();personInfo->name = value["info"][index]["name"].asString();personInfo->num = value["info"][index]["num"].asString();personInfo->gender = value["gender"].asString();personInfo->occupation = value["occupation"].asString();personList.pushBack(personInfo);}}}void SecondViewController::viewDidUnload(){}void SecondViewController::reshapeViewRectDidFinish(){}void SecondViewController::tableViewDidSelectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row){}void SecondViewController::tableViewDidDeselectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row){}CATableViewCell* SecondViewController::tableCellAtIndex(CATableView* table, const CCSize& cellSize, unsigned int section, unsigned int row){CADipSize _size = cellSize;    //根據參數傳入的Section和row在資料中尋找相應資料Info* p_List = (Info*)personList.at(row);    //從table的複用隊列中尋找指定標識符的cell,如果不存在,則返回NULL。CATableViewCell* cell = table->dequeueReusableCellWithIdentifier("CrossApp");    //如果返回的是NULL 則建立一個cell兵添加相應的控制項if (cell == NULL){CCLog("Cell-%d",row);        //建立一個cell 並設定他的標示符為"CrossApp"cell = CATableViewCell::create("CrossApp");        //添加一系列的控制項CALabel* p_Name = CALabel::createWithCenter(CADipRect(_size.width*0.2, _size.height*0.5, _size.width*0.2, _size.height));p_Name->setTag(9);p_Name->setFontSize(_px(30));p_Name->setColor(CAColor_blueStyle);p_Name->setTextAlignment(CATextAlignmentCenter);p_Name->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);cell->addSubview(p_Name);CALabel* p_Num = CALabel::createWithCenter(CADipRect(_size.width*0.4, _size.height*0.5, _size.width*0.2, _size.height));p_Num->setTag(10);p_Num->setFontSize(_px(30));p_Num->setColor(CAColor_blueStyle);p_Num->setTextAlignment(CATextAlignmentCenter);p_Num->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);cell->addSubview(p_Num);CALabel* p_Gender = CALabel::createWithCenter(CADipRect(_size.width*0.6, _size.height*0.5, _size.width*0.2, _size.height));p_Gender->setTag(11);p_Gender->setFontSize(_px(30));p_Gender->setColor(CAColor_blueStyle);p_Gender->setTextAlignment(CATextAlignmentCenter);p_Gender->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);cell->addSubview(p_Gender);CALabel* p_Occupation = CALabel::createWithCenter(CADipRect(_size.width*0.8, _size.height*0.5, _size.width*0.2, _size.height));p_Occupation->setTag(12);p_Occupation->setFontSize(_px(30));p_Occupation->setColor(CAColor_blueStyle);p_Occupation->setTextAlignment(CATextAlignmentCenter);p_Occupation->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);cell->addSubview(p_Occupation);}    //從cell中通過tag擷取控制項,並設定相應參數,修改顯示狀態CALabel* p_Name = (CALabel*)cell->getSubviewByTag(9);p_Name->setText(p_List->name.c_str());CALabel* p_Num = (CALabel*)cell->getSubviewByTag(10);p_Num->setText(p_List->num.c_str());CALabel* p_Gender = (CALabel*)cell->getSubviewByTag(11);p_Gender->setText(p_List->gender.c_str());CALabel* p_Occupation = (CALabel*)cell->getSubviewByTag(12);p_Occupation->setText(p_List->occupation.c_str());return cell;}CAView* SecondViewController::tableViewSectionViewForHeaderInSection(CATableView* table, const CCSize& viewSize, unsigned int section){CADipSize _viewSize = viewSize;char head[10] = "";    //建立一個view 最後將view返回,然後table會自動將這個view 添加到header裡邊CAView* view = CAView::createWithColor(ccc4(239,242,243,255));CAButton* headControl1 = CAButton::createWithCenter(CADipRect(60, _viewSize.height*0.5, 80, 80),CAButtonTypeRoundedRect);headControl1->setTag(100 + (int)section);if (sect[section] == CELL_COUNT){        //設定按鈕狀態的背景圖headControl1->setBackGroundViewForState(CAControlStateNormal, CAImageView::createWithImage(CAImage::create("source_material/close1.png")));}else{headControl1->setBackGroundViewForState(CAControlStateNormal, CAImageView::createWithImage(CAImage::create("source_material/open1.png")));}    //設定按鈕的回調事件    headControl1->addTarget(this, CAControl_selector(SecondViewController::switchCellListInSection), CAControlEventTouchUpInSide);view->addSubview(headControl1);CALabel* header = CALabel::createWithCenter(CADipRect(_viewSize.width*0.5, _viewSize.height*0.5, 300, 50));sprintf(head, "Section-%d", section);header->setFontSize(_px(30));header->setText(head);header->setColor(CAColor_blueStyle);header->setTextAlignment(CATextAlignmentCenter);header->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);view->addSubview(header);return view;}CAView* SecondViewController::tableViewSectionViewForFooterInSection(CATableView* table, const CCSize& viewSize, unsigned int section){CADipSize _viewSize = viewSize;char head[10] = "";CAView* view = CAView::createWithColor(CAColor_blueStyle);   return view;}unsigned int SecondViewController::numberOfRowsInSection(CATableView *table, unsigned int section){    //通過section的編號返回section中有多少行 在demo中是將每個section有多少行儲存在sect數組中,所以這裡 return sect[section]return sect[section];}unsigned int SecondViewController::numberOfSections(CATableView *table){    //返回整個table中一共有多少個Sectionsreturn NUM;}unsigned int SecondViewController::tableViewHeightForRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row){    //返回row的高度是100return _px(100);}unsigned int SecondViewController::tableViewHeightForHeaderInSection(CATableView* table, unsigned int section){    //返回header頭欄的高度是100return _px(100);}unsigned int SecondViewController::tableViewHeightForFooterInSection(CATableView* table, unsigned int section){    //返回尾懶高度是1(仔細看,會發現一個細小的藍色條………………)return 1;}void SecondViewController::scrollViewHeaderBeginRefreshing(CAScrollView* view){    //從最上邊下拉重新整理的時候,將isPullUpRefresh設定為true,然後通過schedule 調用refreshTableViewData方法isPullUpRefresh = true;    //schedule的參數,1:調用函數,2:調用的對象,3:多少秒調用一次,4:在第一次調用之後,調用多少次,5:第一次調用之前延時多少秒,6:是否暫停CAScheduler::schedule(schedule_selector(SecondViewController::refreshTableViewData), this, 0.1, 0, CCRANDOM_0_1() * 2, false);    }void SecondViewController::scrollViewFooterBeginRefreshing(CAScrollView* view){isPullUpRefresh = false;CAScheduler::schedule(schedule_selector(SecondViewController::refreshTableViewData), this, 0.1, 0, CCRANDOM_0_1() * 2, false);}void SecondViewController::refreshTableViewData(float interval){for (int i = 0; i < NUM; i++){//設定sect中的資料以設定table中每個section顯示多少rowsect[i] = (isPullUpRefresh == true) ? 0 : CELL_COUNT;}p_TableView->reloadData();}void SecondViewController::switchCellListInSection(CAControl* btn, CCPoint point){    int section = btn->getTag() - 100;CC_RETURN_IF(section >= NUM);sect[section] = sect[section] ? 0 : CELL_COUNT;btn->retain()->autorelease();p_TableView->reloadData();}void SecondViewController::closeCellListInSection(CAControl* btn, CCPoint point){}<span style="font-size:18px;"><span style="font-family:Arial;color:#333333;"><span style="line-height: 26px;"></span></span></span>


[寒江孤葉丶的CrossApp之旅_11][入門系列]通過Demo學習CrossApp之SecondViewController篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.