Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
What is the easiest way to grab only some info from a .plist file and display it in a table view?

Example:
I have a .plist that has different shops stored in it, with each of the shop's states within the .plist file in a string called "States". Each store has an individual item in the .plist file. I want to have different tab bars displaying different states, with the shops from just that state displayed in the table view.

What is the best way to programmatically manipulate what is displayed from a .plist file in a table view?
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
What is the easiest way to grab only some info from a .plist file and display it in a table view?

Example:
I have a .plist that has different shops stored in it, with each of the shop's states within the .plist file in a string called "States". Each store has an individual item in the .plist file. I want to have different tab bars displaying different states, with the shops from just that state displayed in the table view.

What is the best way to programmatically manipulate what is displayed from a .plist file in a table view?

NSDictionary has a method "dictionaryWithContentsOfFile:" that will load your entire plist at once, if its root is a dictionary. Otherwise you'll have to us NSXMLParser.

Then you want to go from "list of shops, with states for each" to "list of states, with shops for each," right? You'll have to do something like this:

(1) get list of keys from the store-to-state dictionary
(2) loop through the keys; for each key:
(2b) split the string of states into an array
(2c) loop through the state array; for each state:
(2c.1) add the state-to-store pair to another dictionary

Now you have a dictionary you can use in the data source for the table.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I'd suggest not storing this data in the .plist file but using NSUserDefaults (along with an NSDictionary, as suggested above) instead.
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
Unfortunately putting the data into NSUserDefaults is not an option, as I already have data in a .plist file that contains data. Or do you mean to take the data out of a .plist file and store it into NSUserDeafaults?

Whichever way you suggest, do you have some sample code or documentation on the various parts I need to do? I am new to iPhone app development and would like to learn how to do this fairly quickly.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Or do you mean to take the data out of a .plist file and store it into NSUserDeafaults?
Yes, that is what I'm suggesting.

Whichever way you suggest, do you have some sample code or documentation on the various parts I need to do? I am new to iPhone app development and would like to learn how to do this fairly quickly.
Looking up NSUserDefaults in the Developer Documentation, shows me that there are five apps of Related Sample Code: AppPrefs, BubbleLevel, DrillDownSave, Metronome, and MoviePlayer.

Also, although I understand the temptation to want to get results quickly, I'd suggest concentrating on learning how to do it more thoroughly.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I'd suggest not storing this data in the .plist file but using NSUserDefaults (along with an NSDictionary, as suggested above) instead.

I'd fundamentally disagree. NSUserDefaults is intended for storing user preferences that get updated. It is not stored within the app bundle, rather in a writable location. If this is static data (which it sounds like it is) then a .plist (or other) file in the resources of the app is the correct location.

Loading it should be simple using the methods in NSDictionary or NSArray (depending on the root element).
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Whether the data are in a dictionary in a plist file or a dictionary in NSUserDefaults really isn't important.

Work from your table backwards. What data structure do you need to populate your tableview? Write code that reads your plist and builds that data structure.

I usually use an array of arrays of dictionaries for a sectioned tableview. The dictionary represents a row, the array that holds the dictionaries represents a section, and the top level array holds all the sections. In your case the section array would represent the states.
 

shsgator81

macrumors newbie
Mar 14, 2009
1
0
Whether the data are in a dictionary in a plist file or a dictionary in NSUserDefaults really isn't important.

Work from your table backwards. What data structure do you need to populate your tableview? Write code that reads your plist and builds that data structure.

I usually use an array of arrays of dictionaries for a sectioned tableview. The dictionary represents a row, the array that holds the dictionaries represents a section, and the top level array holds all the sections. In your case the section array would represent the states.

Could anyone point me to a good example (or several) of using NSDictionary that retrieves a multi-level set of keys/values from a plist? Before going the SQLite route, I would like to test traversing my data using the array of arrays approach suggested here.

Also, what is the best approach to "retaining" this data across several view controllers? As you can no doubt tell, I am an iPhone dev newbie.

Many thanks in advance for your help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.