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

原創文章,歡迎轉載,轉載請註明:文章來自[寒江孤葉丶的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 8
class 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的頭欄,
    //這個函數有點類似於上邊的tableCellAtIndex
	virtual 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中有多少個Section
	virtual 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;
    //是否是頂部刷新的flag
	bool 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大小相同的TableView
	p_TableView = CATableView::createWithFrame(this->getView()->getBounds());
    //將數據類設置爲this(因爲本身這個類繼承自TableViewDataSource )
	p_TableView->setTableViewDataSource(this);
    //將table的觸發事件託管類設置爲this
	p_TableView->setTableViewDelegate(this);
    //設置爲可選模式   還可以設置爲多選模式 setAllowsMultipleSelection
	p_TableView->setAllowsSelection(true);
    //設置table的滾動view 的事件託管類(table的滾動顯示功能是因爲table繼承自scrollview)
	p_TableView->setScrollViewDelegate(this);
    //設置頭部向下拖動刷新顯示的view
	p_TableView->setHeaderRefreshView(headerRefreshView);
    //設置尾部向下拖動刷新顯示的view
	p_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中一共有多少個Sections
	return NUM;
}

unsigned int SecondViewController::tableViewHeightForRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row)
{
    //返回row的高度是100
	return _px(100);
}

unsigned int SecondViewController::tableViewHeightForHeaderInSection(CATableView* table, unsigned int section)
{
    //返回header頭欄的高度是100
	return _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顯示多少row
		sect[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>


發佈了55 篇原創文章 · 獲贊 3 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章