新手CrossApp 之demo SecondViewController小结

<span style="font-size:24px;color:#cc6600;">SecondViewController</span>继承了许多的类:

               CAViewController,  视图控制类
               CATableViewDataSource, tableView数据控制类
               CATableViewDelegate,   tableView事件代理     
               CAScrollViewDelegate   scrollView事件代理
实现了许多虚函数方法,和定义了一些私有变量
        CADipSize size;      //视图大小
<span style="white-space:pre">	</span>CATableView* p_TableView;  //一个tableView,用来创建
<span style="white-space:pre">	</span>int sect[NUM];             //#define NUM 8 一个sect数组
<span style="white-space:pre">	</span>CADeque<Info*> personList; // 一个CADeque<Info*>
<span style="white-space:pre">	</span>bool isPullUpRefresh;
         

首先还是来看ViewDidLoad() 这个方法,方法主要是加载一些视图控件

<pre name="code" class="cpp">void SecondViewController::viewDidLoad(){
	loadJsonData();
	//上下刷新视图的创建
	CAPullToRefreshView* headerRefreshView = CAPullToRefreshView::create(CAPullToRefreshView::CAPullToRefreshTypeHeader);
	CAPullToRefreshView* footerRefreshView = CAPullToRefreshView::create(CAPullToRefreshView::CAPullToRefreshTypeFooter);
	//创建了一个tableView,tableView每一行只有一个cell
	p_TableView = CATableView::createWithFrame(this->getView()->getBounds());
	p_TableView->setTableViewDataSource(this);  //设置数据代理
	p_TableView->setTableViewDelegate(this);    //设置事件处理
	p_TableView->setTableHeaderView(headerRefreshView); //设置头部刷新视图
	p_TableView->setTableFooterView(footerRefreshView); //设置尾部刷新视图
	p_TableView->setAllowsSelection(true);              //设置是否可选
	p_TableView->setScrollViewDelegate(this);           //设置scroll代理
	this->getView()->addSubview(p_TableView);           //添加进主视图
}

tableCellAtIndex这个方法是在tableview中创建创建
 
<pre name="code" class="cpp">CATableViewCell* SecondViewController::tableCellAtIndex(CATableView* table, const CCSize& cellSize, unsigned int section, unsigned int row){
	CCSize _size = cellSize;
	Info* p_List = (Info*)personList.at(row);//CADeque<Info*> personList中寻找第几号元素
	CATableViewCell* cell = table->dequeueReusableCellWithIdentifier("CrossApp");//寻找有没有"CrossApp"的cell,肯定是没有的所以每次都会进入if
	if (cell==NULL)
	{
		//_size.width*0.2, _size.height*0.5, _size.width*0.2, _size.height
		cell = CATableViewCell::create("CrossApp");//创建
		CALabel* p_name=CALabel::createWithCenter(CCRect(_size.width*0.2, _size.height*0.5, _size.width*0.2, _size.height));//创建一个lable
		p_name->setTag(9);                        //设置标记
		p_name->setFontSize(_px(30));
		p_name->setColor(CAColor_blue);
 		p_name->setTextAlignment(CATextAlignmentCenter);
		p_name->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		cell->addSubview(p_name);                 //添加进去   

		CALabel* p_num = CALabel::createWithCenter(CCRect(_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_blue);
		p_num->setTextAlignment(CATextAlignmentCenter);
		p_num->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		cell->addSubview(p_num);

		CALabel* p_gender = CALabel::createWithCenter(CCRect(_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_blue);
		p_gender->setTextAlignment(CATextAlignmentCenter);
		p_gender->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		cell->addSubview(p_gender);

		CALabel* p_occupation = CALabel::createWithCenter(CCRect(_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_blue);
		p_occupation->setTextAlignment(CATextAlignmentCenter);
		p_occupation->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		cell->addSubview(p_occupation);
	}
	CALabel* p_name = (CALabel*)cell->getSubviewByTag(9);    //获取label 标记
	p_name->setText(p_List->name.c_str());                   //设置label的数据类容,这样的数据类容肯定为空 
	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;

}
接下来 personList 这个CADeque<Info*>数据从哪里来的
void SecondViewController::loadJsonData(void){
	
	Reader reader;
	Value value;
	//首先要找到一个名字叫做information.json文件,这个文件在source目录下
	string jsonFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("information.json");
	//把文件里的类容变成CCString 格式
	CCString* jsonData = CCString::createWithContentsOfFile(jsonFile.c_str());
	//解析这种格式,放入value中
	if (reader.parse(jsonData->getCString(),value))
	{
		//有一个Info.h类,里面有4个
		//std::string name;
		//std::string num;
		//std::string gender;
		//std::string occupation;


		int length = value["info"].size();
		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);  //放入personList
		}
	}
}




                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值