Recently in the product has such a demand, read the local JSON file of the country and city information, displayed on the Pickerview, on-line check, found that there is no suitable resources available, so they wrote a simple demo.
:
Here's how to read local JSON:
+ (Nsmutablearray *) getcitydata{ Nsarray *jsonarray = [[Nsarray alloc]init]; NSData *filedata = [[NSData alloc]init]; Nsuserdefaults *ud = [Nsuserdefaults standarduserdefaults]; if ([UD objectforkey:@ "city"] = = nil) { nsstring *path; Path = [[NSBundle mainbundle] pathforresource:@ "ZH_CN" oftype:@ "json"]; FileData = [NSData Datawithcontentsoffile:path]; [UD setobject:filedata forkey:@ "City"]; [UD synchronize]; } else { FileData = [UD objectforkey:@ "City"]; } Nsmutablearray *array = [[Nsmutablearray alloc]initwithcapacity:0]; Jsonarray = [nsjsonserialization jsonobjectwithdata:filedata options:nsjsonreadingmutableleaves Error:nil]; For (Nsdictionary *dict in Jsonarray) { [array addobject:dict]; } return array;}
Citypicker main code is as follows:
Returns the number of columns displayed-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{if (Pickerview = = Self.citypicker) { return 3; } else return 1;} Returns the number of rows displayed in the current column-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{if (CO Mponent = = 0) {return [ymutils getcitydata].count; } else if (component = = 1) {Nsarray *array = [ymutils getcitydata][row1][@ "Children"]; if ((nsnull*) Array! = [NSNull null]) {return array.count; } return 0; } else {Nsarray *array = [ymutils getcitydata][row1][@ "Children"]; if ((nsnull*) Array! = [NSNull null]) {Nsarray *array1 = [ymutils getcitydata][row1][@ "Children"][row2][@ "child Ren "]; if (nsnull*) array1! = [NSNull null]) {return array1.count; } return 0; } return 0; }}//set the contents of the current line-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (NSINteger) Row forcomponent: (Nsinteger) component{if (component = = 0) {return [ymutils getcitydata][row][@ "name" ]; } else if (component = = 1) {return [ymutils getcitydata][row1][@ "Children"][row][@ "name"]; } else if (component = = 3) {return [ymutils getcitydata][row1][@ "Children"][row2][@ "Children"][row][@ "name"]; } return nil; }//selected number of rows-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component {if (component = = 0) {row1 = row; Row2 = 0; row3 = 0; [Self.citypicker reloadcomponent:1]; [Self.citypicker Reloadcomponent:2]; } else if (component = = 1) {row2 = row; row3 = 0; [Self.citypicker Reloadcomponent:2]; } else {row3 = row; } Nsinteger cityRow1 = [Self.citypicker selectedrowincomponent:0]; Nsinteger cityRow2 = [Self.citypicker selectedrowincomponent:1]; Nsinteger CITYROW3 = [Self.citypIcker Selectedrowincomponent:2]; nsmutablestring *str = [[nsmutablestring alloc]init]; [Str appendstring:[ymutils getcitydata][cityrow1][@ "name"]; Nsarray *array = [ymutils getcitydata][cityrow1][@ "Children"]; if ((nsnull*) Array! = [NSNull null]) {[str appendstring:[ymutils getcitydata][cityrow1][@ "Children"][cityrow2][@ "n Ame "]); Nsarray *array1 = [ymutils getcitydata][cityrow1][@ "Children"][cityrow2][@ "children"]; if (nsnull*) array1! = [NSNull null]) {[str appendstring:[ymutils getcitydata][cityrow1][@ ' children '][cityrow2 ][@ "Children"][cityrow3][@ "name"]; }} self.cityLabel.text = str;} Text style displayed per line-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) Component Reusingview: (UIView *) view{UILabel *titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, 107, 30 )]; Titlelabel.textalignment = Nstextalignmentcenter; Titlelabel.font = [Uifont systemfontofsize:14];Titlelabel.backgroundcolor = [Uicolor Clearcolor]; if (component = = 0) {Titlelabel.text = [ymutils getcitydata][row][@ "name"]; } else if (component = = 1) {titlelabel.text = [ymutils getcitydata][row1][@ "Children"][row][@ "name"]; } else {titlelabel.text = [ymutils getcitydata][row1][@ "Children"][row2][@ "Children"][row][@ "name"]; } return Titlelabel; }
Demo: http://download.csdn.net/detail/u011918080/8200985
iOS Country City Selector read local JSON file