注册

新手CrossApp 之demo SecondViewController小结

SecondViewController继承了许多的类: CAViewController, 视图控制类 CATableViewDataSource, tableView数据控制类 CATableViewDelegate, tableView事件代理 CAScrollViewDelegate scrollView事件代理 实现了许多虚函数方法,和定义了一些私有变量 CADipSize size; //视图大小
CATableView* p_TableView; //一个tableView,用来创建
int sect; //#define NUM 8 一个sect数组
CADeque personList; // 一个CADeque
bool isPullUpRefresh;
首先还是来看ViewDidLoad() 这个方法,方法主要是加载一些视图控件

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中创建创建
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 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数据从哪里来的



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.size();
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); //放入personList
}
}
}



已邀请:

要回复问题请先登录注册