Original article, welcome reprint, Reprint please specify: article from [Cold River solitary Ye Shi Crossapp Tour Series]
Blog Address: http://blog.csdn.net/qq446569365
This article is I read the demo time to write the notes, share out for everyone to exchange discussion. If there is no place to welcome the point!
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 8class Secondviewcontroller:public Caviewcontroller, Catableviewdelegate, catableviewdatasource,cascrollviewdelegate{ Public://destructor Secondviewcontroller (); virtual ~secondviewcontroller (); Protected://life cycle callback function void Viewdidload (); void Viewdidunload (); virtual void viewdidappear (); Read data from JSON loadjsondata (void);p ublic://monitor the size of the root view (parent node view) changes if the current Viewcontroller's root view You can then listen to this interface to get the changed size. virtual void reshapeviewrectdidfinish (); The callback message that is generated when the table item is selected, three parameters are the table that triggered the event, the section where the item is clicked (which can be understood as a partition), and the first line of the clicked item in the partition virtual void Tableviewdidselectrowatindexpath (catableview* table, unsigned int section, unsigned int row); Basically ibid., only selected items changed to uncheck the item virtual void Tableviewdiddeselectrowatindexpath (catableview* table, unsigned int section, unsigned int row); Create a cell for TableView, in a reusable way, when scrolling the table, it will callback the function, in this function, set the table item display data, a bit similar to the form of Android virtual catableviewcell* Tablecellatindex (catableview* table, const ccsize& cellsize, unsigned int section, unsigned int row); This function is used to set the header contents of each section, three parameters the first is which table the callback is, the second is the size of the header, and the third is which section header,// This function is somewhat similar to the upper tablecellatindexvirtual caview* tableviewsectionviewforheaderinsection (catableview* table, const CCSize & viewsize, unsigned int section);//The same as above, but this is the end column of virtual caview* tableviewsectionviewforfooterinsection ( catableview* table, const ccsize& viewsize, unsigned int section);//returns how many rows are returned in the sections, based on the number of sections, How many rows will be drawn when the table is plotted virtual unsigned int numberofrowsinsection (Catableview *table, unsigned int section); How many sectionvirtual unsigned int numberofsections (Catableview *table) are in the table? Returns the height of row to table, which sets the height of the column in the table by returning different values based on the row and section passed in at the time of the callback unsigned int tableviewheightforrowatindexpath ( catableview* table, unsigned int section, unsigned intRow); Basic Ibid., this is the height of the Set header column virtual unsigned int tableviewheightforheaderinsection (catableview* table, unsigned int section); Basic Ibid., set the height of the tail bar virtual unsigned int tableviewheightforfooterinsection (catableview* table, unsigned int section); Cascrollviewdelegate callback function that triggers virtual void scrollviewheaderbeginrefreshing (cascrollview* view) when the user refreshes from the top dropdown; The basic above, but the bottom drop-down refresh when triggered by the virtual void scrollviewfooterbeginrefreshing (cascrollview* view);p ublic://Refresh the data function (for Scrollviewfooterbeginrefreshing and scrollviewheaderbeginrefreshing Trigger when he refreshes the list) the user can set it up at development time to retrieve the latest data from the network after the callback refreshes void Refreshtableviewdata (float interval); Section has a button, click to expand, click to close, this is the button's click callback function void Switchcelllistinsection (cacontrol* btn,ccpoint point); A function of unknown sensation, which has now been discarded void closecelllistinsection (cacontrol* btn, Ccpoint point);p rivate:cadipsize size; catableview* P_tableview; Sect is used to store the number of rows in each section, that is, how many lines int sect[num]; Cadeque<info*> personlist for storing data; Is the top refresh of the Flagbool Ispulluprefresh;}; #endif
SecondViewController.cpp
#include "SecondViewController.h" #define Cacolor_bluestyle ccc4 (51,204,255,255) #define Cell_count 16//Constructor, Ispulluprefresh is set to True and then initializes all data in sect to Cell_count (that is, 16), which is: Set all sections to the expanded state Secondviewcontroller:: Secondviewcontroller (): Ispulluprefresh (True) {for (int i = 0; i < NUM; i++) {sect[i] = Cell_count;}} destructor, emptying personlist data Secondviewcontroller::~secondviewcontroller () {personlist.clear ();} void Secondviewcontroller::viewdidload (void) {loadjsondata ();//Initialize the displayed data, here is the data to be read from the JSON to display in the table suggested to follow in to see// Create Capulltorefreshview Drag to refresh the view,ca really convenient ah ... Capulltorefreshview is the display type of the view, with three types, header, tail, and custom capulltorefreshview* Headerrefreshview = Capulltorefreshview::create ( Capulltorefreshview::capulltorefreshtypeheader); capulltorefreshview* Footerrefreshview = Capulltorefreshview::create (Capulltorefreshview:: Capulltorefreshtypefooter); Create a tableviewp_tableview with the same view size = Catableview::createwithframe (This->getview ()->getbounds ()); Set the data class to this (because the class itself inherits from Tableviewdatasource) P_tableview->settabLeviewdatasource (this); Set the trigger event managed class for table to Thisp_tableview->settableviewdelegate (this); Set to optional mode can also be set to multi-select mode Setallowsmultipleselectionp_tableview->setallowsselection (TRUE); The event-managed class that sets the scrolling view for table (the scrolling display function of table is because table inherits from ScrollView) P_tableview->setscrollviewdelegate (this); Set the head down drag to refresh the displayed Viewp_tableview->setheaderrefreshview (Headerrefreshview); Set the trailing downward drag to refresh the displayed Viewp_tableview->setfooterrefreshview (Footerrefreshview); This->getview ()->addsubview (p _tableview);} void Secondviewcontroller::viewdidappear () {//Set the top 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 ();p Ersoninfo->autoreleas E ();p ersoninfo->name = value["info"][index]["name"].asstring ();p ersoninfo->num = value["info"][index]["num"] . asstring ();p Ersoninfo->gender = value["Gender"].asstring ();p ersoninfo->occupation = value["occupation"]. Asstring ();p ersonlist.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; info* p_list = (info*) personlist.at (row) is found in the data based on the section and row passed in by the parameter;Looks for the cell with the specified identifier from the reuse queue of the table and returns null if it does not exist. catableviewcell* cell = Table->dequeuereusablecellwithidentifier ("Crossapp"); If NULL is returned, create a cell to add the appropriate control if (cell = = NULL) {cclog ("cell-%d", Row); Create a cell and set his identifier as "crossapp" cell = Catableviewcell::create ("Crossapp"); Add a series of controls 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 ());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 ();p _num->setfontsize (_px ());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 (one);p _gender->setfontsize (_px ());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 (;p _occupation->setfontsize (_px ());p _occupation->setcolor ( Cacolor_bluestyle);p _occupation->settextalignment (catextalignmentcenter);p _occupation-> Setverticaltextalignmet (Caverticaltextalignmentcenter); Cell->addsubview (p_occupation);} Get the control from the cell via tag and set the corresponding parameters, modify the display state calabel* P_name = (calabel*) Cell->getsubviewbytag (9);p _name->settext (p_ List->name.c_str ()); calabel* p_num = (calabel*) Cell->getsubviewbytag (Ten);p _num->settext (p_list->nUm.c_str ()); calabel* P_gender = (calabel*) cell->getsubviewbytag (one);p _gender->settext (P_list->gender.c_str ()); calabel* p_occupation = (calabel*) cell->getsubviewbytag (;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] = ""; Create a view to return the view at the end, and the table will automatically add the view to the header caview* view = Caview::createwithcolor (CCC4 (239,242,243,255)); cabutton* headControl1 = Cabutton::createwithcenter (Cadiprect (_viewsize.height*0.5, 80, 80), Cabuttontyperoundedrect) Headcontrol1->settag (+ + (int) section), if (sect[section] = = Cell_count) {//Set button state background image Headcontrol1->setbackgroundviewforstate (Cacontrolstatenormal, Caimageview::createwithimage (CAImage::create (" Source_material/close1.png ")));} Else{headcontrol1->setbackgroundviewforstate (Cacontrolstatenormal, Caimageview::Createwithimage (Caimage::create ("source_material/open1.png"));} Sets the callback event for the button Headcontrol1->addtarget (this, Cacontrol_selector (secondviewcontroller::switchcelllistinsection), Cacontroleventtouchupinside); View->addsubview (headControl1); calabel* Header = Calabel::createwithcenter (Cadiprect (_viewsize.width*0.5, _viewsize.height*0.5, +)); sprintf ( Head, "section-%d", section); Header->setfontsize (_px); 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) {//The number of rows in the section is returned by the section, in the demo, how many rows per section are stored in the sect array, so return Sect[section]return sect[section];} unsigned int secondviewcontroller::numberofsections (Catableview *table) {//Returns the total number of Sectionsreturn NUM in the entire table;} unsigned int secondviewcontroller::tableviewheightforrowatindexpath (catableview* table, unsigned int section, unsigned int row) {//return row height is 100return _px (100);} unsigned int secondviewcontroller::tableviewheightforheaderinsection (catableview* table, unsigned int section) {// The height of the header column returned is 100return _px (100);} unsigned int secondviewcontroller::tableviewheightforfooterinsection (catableview* table, unsigned int section) {// Return to the end of the lazy height is 1 (look closely, you will find a small blue bar ...). ) return 1;} void Secondviewcontroller::scrollviewheaderbeginrefreshing (cascrollview* view) {//From the top drop-down refresh, Set Ispulluprefresh to True, and then call the Refreshtableviewdata method via schedule Ispulluprefresh = true; Schedule parameters, 1: Call function, 2: Called object, 3: How many seconds to call, 4: How many times after the first call, 5: How many seconds to delay before the first call, 6: Whether to pause 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++) {// Set the data in sect to set how many rowsect[i] = (Ispulluprefresh = = true) for each section in the table? 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>
[The Crossapp journey of the lone Ye Shi in the Cold River _11] [Introductory series] through the Demo Learning Crossapp Secondviewcontroller Chapter