Parse the plist file (the dictionary contains the array, and the dictionary is also included)

Source: Internet
Author: User

1 #import "RootTableViewController.h"2 #import "City.h"3 4 @interfaceRoottableviewcontroller ()5 6 //Declare a large dictionary that holds all cities7@property (nonatomic, strong) Nsmutabledictionary *alldatadict;8 9 //store all the key arrays (storage provinces)Ten@property (nonatomic, strong) Nsmutablearray *Allkeysarray; One  A @end -  - @implementationRoottableviewcontroller the  -  - //Lazy Loading: Use to create again, can save memory --(Nsmutabledictionary *) alldatadict { +      -     if(_alldatadict = =Nil) { +_alldatadict =[Nsmutabledictionary dictionary]; A     } at     return_alldatadict; - } -  --(Nsmutablearray *) Allkeysarray { -      -     if(!_allkeysarray) { in_allkeysarray =[Nsmutablearray array]; -     } to     return_allkeysarray; + } -  the  *  $- (void) Viewdidload {Panax Notoginseng [Super Viewdidload]; -      the     //Read plist file + [self readplist]; A } the  +  -  $ //Read plist file $- (void) readplist { -      -     //1. Get the path to the file ([NSBundle Mainbundle] Get the repository) theNSString *filepath = [[NSBundle mainbundle] Pathforresource:@" City"OfType:@"plist"]; -     Wuyi      the     //2. Get data based on path -Nsdictionary *datadict =[Nsdictionary Dictionarywithcontentsoffile:filepath]; Wu      -     //NSLog (@ "%@", datadict); About      $      -     //3. Traversing a dictionary -      for(NSString *keyinchdatadict) { -         //NSLog (@ "%@", Datadict[key]); A          +         //creates a temporary array to hold the model object theNsmutablearray *cityarray =[Nsmutablearray array]; -          $          the         //traversing a city array the          for(Nsdictionary *dictinchDatadict[key]) { the              the             //3.1 Creating a data Model -City *city =[[City alloc] init]; in              the             //3.2 Assigning values using KVC the [City setvaluesforkeyswithdictionary:dict]; About              the             //3.3 Add to array the [Cityarray addobject:city]; the              +         } -          the         Bayi         //Save a group of cities to a large dictionary the [Self.alldatadict Setobject:cityarray Forkey:key]; the          -         //Save the key in the array - [Self.allkeysarray Addobject:key]; the          the     } the } the  -  the  the #pragmaMark-table View Data source the 94 //set Number of partitions the-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView { the      the     returnSelf.allKeysArray.count;98 } About  - 101 //set the number of rows per partition102-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {103     104     //Find the City key (province) theNSString *key =Self.allkeysarray[section];106     107     //find an array of cities108Nsarray *cityarray =[Self.alldatadict Objectforkey:key];109      the     returnCityarray.count;111 } the 113  the //Return to Cell the-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { the     117     StaticNSString *identifier =@"Cell";118     119UITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:identifier]; -     121     if(!cell) {122Cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:identifier];123     }124      the     126     //Get key127NSString *key =Self.allkeysarray[indexpath.section]; -     129     //get the city array based on key theNsarray *cityarray =[Self.alldatadict Objectforkey:key];131      the     //gets the model in the city array to assign the value133City *city =Cityarray[indexpath.row];134Cell.textLabel.text =City.name;135Cell.detailTextLabel.text =City.personnum;136 137     138     returncell;139 } $ 141 @end

Parse the plist file (the dictionary contains the array, and the dictionary is also included)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.