新手CrossApp 之demo SecondViewController小结

来源:互联网 发布:淘宝包邮拒签邮费谁出 编辑:程序博客网 时间:2024/05/17 13:40
<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每一行只有一个cellp_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,肯定是没有的所以每次都会进入ifif (cell==NULL){//_size.width*0.2, _size.height*0.5, _size.width*0.2, _size.heightcell = CATableViewCell::create("CrossApp");//创建CALabel* p_name=CALabel::createWithCenter(CCRect(_size.width*0.2, _size.height*0.5, _size.width*0.2, _size.height));//创建一个lablep_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
原创粉丝点击