ios - How do I drill down through a plist using UITableViews? -


i'm wanting drill down through plist using uitableviews specific school. drill down goes state->district->school. i've created plist, not 100% sure structure best. also, can first set of information active on first tableview, not sure how proceed there. need create tableview each drill down(stateview, districtview, schoolview) or can reuse generic tableview since simple lists? below have far. help.

plist <plist version="1.0"> <array> <dict>     <key>districts</key>     <dict>         <key>district 1</key>         <array>             <string>school 2</string>             <string>school 1</string>         </array>         <key>district 2</key>         <array>             <string>school 3</string>             <string>school 4</string>         </array>     </dict>     <key>state</key>     <string>south dakota</string> </dict> <dict>     <key>districts</key>     <array>         <string>district 1</string>         <string>district 2</string>     </array>     <key>state</key>     <string>arkansas</string> </dict> <dict>     <key>districts</key>     <array>         <string>district 3</string>         <string>district 4</string>     </array>     <key>state</key>     <string>new york</string> </dict> </array> </plist> 

and here viewcontroller

#import "plistviewcontroller.h"  @interface plistviewcontroller ()  @end  @implementation plistviewcontroller  - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) {  } return self; }  @synthesize content = _content;  -(nsarray *)content { if (!_content) {     _content = [[nsarray alloc] initwithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"data" oftype:@"plist"]]; } return _content; }  - (void)viewdidload { [super viewdidload];  }  - (void)didreceivememorywarning { [super didreceivememorywarning];  }  #pragma mark - table view data source  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {  return [self.content count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];  cell.textlabel.text = [[self.content objectatindex:indexpath.row] valueforkey:@"state"]; return cell;  }   #pragma mark - table view delegate  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. }  @end 

the best thing uitableview doesn't care data displays. asks delegate few different questions:

  1. how many sections should have?
  2. how many rows in each section?
  3. may have uitableviewcell _ index path?

so, have focus on making delegate responses provide correct data.

so, first split plist manageable chunks. uitableview prima-donna datasource nsarray. neatly maps tableviews because of indexing logic.

that said, first tableviewcontroller plistviewcontroller has logic displaying info. specifically, querying nsdictionary @ array position x , asking return state object. 3 dictionary objects, 3 strings returned. nice.

so how go next level? tableview here. asks specific question of delegate:

  1. what do when user touches section y row x?

you're going need set uitableviewcontroller subclass called districtviewcontroller. in header .h file, going need make strong property nsdictionary object. so:

//districtviewcontroller.h @interface districtviewcontroller : uitableviewcontroller @property (nonatomic, strong) nsdictionary *districtdictionary; @end  //districtviewcontroller.m @implementation districtviewcontroller @synthesize districtdictionary; 

and there have it. class set keep track of 1 nsdictionary object. have configure table delegate methods show data want.

the first example, in top row (index:0) of nsarray, have dictionary has 2 keys: district 1 , district 2. problem. nsdictionary doesn't map tableviews quite easily, because nsdictionary objects don't use indexes work. don't fret. nsdictionary has method called allkeys, give array of each key in dictionary. useful when receiving nsdictionary somewhere not know keys contains beforehand.

so, questions tableview asks, let's answer them:

//how many sections in me: let's 1 now. //how many rows in section:  //ask nsdictionary how many keys has: nsarray *keyarray = [self.districtdictionary allkeys]; return [keyarray count];  //give me tablecell index path x,y   //first, array of keys back: nsarray *keyarray = [self.districtdictionary allkeys]; //next, find key given table index: nsstring *mykey = [keyarray objectatindex:indexpath.row]; //finally, display string in cell: cell.textlabel.text = mykey; 

after this, you'd same thing final view. set viewcontroller schools , call schoolviewcontroller , make set in charge of nsarray. before:

@interface schoolviewcontroller : uitableviewcontroller @property (nonatomic, strong) nsarray *schoolarray; @end  @implementation schoolviewcontroller @synthesize schoolarray; 

in view, lot first. have viewcontroller answer table's questions before:

  1. how many sections? need 1
  2. how many rows? need many in array return [schoolarray count];
  3. give me cell: cell.textlabel.text = [schoolarray objectatindex:indexpath.row];

the final piece puts in final question table asks.

  1. what do when user touches row?

in each view, @ method signature:

-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { 

this add logic hook things up. in first view plistviewcontroller, this:

nsdictionary *topleveldictionary = [self.content objectatindex:indexpath.row]; nsdictionary *alldistricts = [topleveldictionary objectforkey:@"districts"]; districtviewcontroller *dview = [[districtviewcontroller alloc] initwithstyle:uitableviewstyleplain]; dview.districtdictionary = alldistricts; [self.navigationcontroller pushviewcontroller:dview animated:yes]; 

in second view, districtviewcontroller this:

nsarray *keyarray = [self.districtdictionary allkeys]; nsstring *mykey = [keyarray objectatindex:indexpath.row]; nsarray *schoolarray = [self.districtdictionary objectforkey:mykey]; schoolviewcontroller *sview = [[schoolviewcontroller alloc]initwithstyle:uitableviewstyleplain]; sview.schoolarray = schoolarray; [self.navigationcontroller pushviewcontroller:sview animated:yes]; 

i hope helps you. typed in plain text editor. there's no misspellings. you'll need #import associated viewcontrollers in each one! luck.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -