注册

[寒江孤叶丶的CrossApp之旅_11][入门系列]通过Demo学习CrossApp...

通过Demo学习CrossApp之SecondViewController篇

本文章是我在读Demo时候随手写的注释,分享出来供大家交流探讨。如有不对之处欢迎指出!

SecondViewController.h

#ifndef _Second_ViewController_h_
#define _Second_ViewController_h_

#include
#include "CrossApp.h"
#include "CrossAppExt.h"
#include "Info.h"

USING_NS_CC_EXT;

using namespace CSJson;
#define NUM 8
cl** 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);

**:
CADipSize size;
CATableView* p_TableView;
//sect是用于存储每个section中有多少个row 嘛,就是多少行
int sect;
//用于存储数据的
CADeque 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 = 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()
{
//设置上边的那条
CANavigationBar*** ** = CANavigationBar**::create("ViewController2");
this->getTabBarController()->setNavigationBar**(**);
}

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.size();
CCLog("%d",length);
for (int index = 0; index < length; index++)
{
Info* personInfo = new Info();
personInfo->autorelease();
personInfo->name = value.**tring();
personInfo->num = value.**tring();
personInfo->gender = value.**tring();
personInfo->occupation = value.**tring();
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 = "";
//创建一个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 == 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), CAControl**TouchUpInSide);
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 = "";
CAView* view = CAView::createWithColor(CAColor_blueStyle);

return view;
}

unsigned int SecondViewController::numberOfRowsInSection(CATableView *table, unsigned int section)
{
//通过section的编号返回section中有多少行 在demo中是将每个section有多少行存储在sect数组中,所以这里 return sect
return sect;
}

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 = (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 = sect ? 0 : CELL_COUNT;
btn->retain()->autorelease();
p_TableView->reloadData();
}

void SecondViewController::closeCellListInSection(CAControl* btn, CCPoint point)
{

}














已邀请:

要回复问题请先登录注册