How To Fetch Data Using Plist (Property list)?

This is the “Plist” example. There are many ways to implement the Plist in iPhone. I am going to show you the simplest way to create the Plist in iPhone.

Step 1: Open the Xcode and create a new Xcode project using Navigation base application template. Give the application name “Plistinfo”. As shown in the figure below:

Step 2: Expand classes and notice Interface Builder created the RootViewController.h and RootViewController.m class for you. Expand Resources and notice the template generated a separate nib, RootViewController.xib.

Step 3: In this application, we need to add Plist. Just Right-click on the Resources folder and choose Add -> New File. Under Other category, choose Property List. Name it Data.plist. Change Dictionary to Array since we’re going to be adding numerous “namelist” which will be described as individual Dictionaries.

We will be adding information about your Datalist collection to it so the schema of your data set could be something like this:
* name – String
* add – String
* ph – Number

Step 4: We need to add another file. Right-click on the Classes folder and choose Add -> New File. Under Cocoa Touch Class category choose Objective-C class. Name it Dfetch.h and Dfetch.m file.
This will be a very simple class that will take our dummy data file, read it in as a NSArray and provide some utility methods to access the data from the file.

#import <foundation/Foundation.h>

@interface Dfetch : NSObject {
NSString *libraryPlist;
NSArray *libraryContent;
}

@property (nonatomic, readonly) NSString *libraryPlist;
@property (nonatomic, readonly) NSArray *libraryContent;

- (id)initWithLibraryName:(NSString *)libraryName;
- (NSDictionary *)libraryItemAtIndex:(int)index;
- (int)libraryCount;

@end

 

Step 5: Open the Dfetch.m file and make the following changes in the file.

#import "Dfetch.h"
@implementation Dfetch
@synthesize libraryContent, libraryPlist;

- (id)initWithLibraryName:(NSString *)libraryName {
if (self = [super init]) {
libraryPlist = libraryName;
libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:libraryPlist ofType:@"plist"]];
}
return self;
}

- (NSDictionary *)libraryItemAtIndex:(int)index {
return (libraryContent != nil && [libraryContent count] > 0 && index < [libraryContent count]) ? [libraryContent objectAtIndex:index] : nil; } - (int)libraryCount { return (libraryContent != nil) ? [libraryContent count] : 0; [/code] Step 6: Open the RootViewController.h file and make the following changes in the file.

#import "Dfetch.h"

@interface RootViewController : UITableViewController {
Dfetch *dao;
}
@end

 

 

Step 7: Open the RootViewController.m file and make the following changes in the file.

- (void)viewWillAppear:(BOOL)animated {
dao = [[Dfetch alloc] initWithLibraryName:@"Data"];
self.title = @"Data List";
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dao libraryCount];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"LibraryListingCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];

return cell;
}

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewStyleGrouped table view style will cause the table have a textured background
// and each section will be separated from the other ones.
DetailViewController *controller = [[DetailViewController alloc]
initWithStyle:UITableViewStyleGrouped
andDvdData:[dao libraryItemAtIndex:indexPath.row]];
controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}

 

 

Step 8: Create The Detail view:
After a user selects one of the rows, we want them to go to a detail page where more information about the Data List is shown. We could simply use a UIView and add a bunch of UILables to it to achieve.

Right-click on the Classes folder and choose Add -> New File again. From Cocoa Touch Class category pick the UIViewControllerSubclass.

#import <uikit/UIKit.h>

@interface DetailViewController : UITableViewController {
NSMutableArray *labels;
NSMutableArray *values;
}

- (id)initWithStyle:(UITableViewStyle)style andDvdData:(NSDictionary *)dvdData;
@end

 

 

Step 9: Open the DetailViewController.m file and make the following changes.

#import "DetailViewController.h"

@implementation DetailViewController

- (id)initWithStyle:(UITableViewStyle)style andDvdData:(NSDictionary *)dvdData {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if (self = [super initWithStyle:style]) {
labels = [NSMutableArray new];
values = [NSMutableArray new];

// Set up labels which will become header titles
[labels addObject:@"Name"];
[labels addObject:@"Address"];
[labels addObject:@"Phone"];

// Add now the values that correspond to each label
[values addObject:[dvdData valueForKey:@"name"]];
[values addObject:[NSString stringWithFormat:@"%@", [dvdData valueForKey:@"add"]]];
[values addObject:[NSString stringWithFormat:@"%@", [dvdData valueForKey:@"ph"]]];
}
return self;
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
return [labels objectAtIndex:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [labels count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"DetailViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [values objectAtIndex:indexPath.section];
return cell;
}

 

 

Step 10: Now build and run the code and view the Output in the Simulator.

You can download source code from here Plist

Leave a Comment:

10 comments
Enrico says April 17, 2011

How can I set Grouped a dataList tableView?

Reply
Trisect Development says April 20, 2011

If I try this and add 4 plist files the last one always return empty. Even if its a copy of the others.

Reply
Trisect Development says April 20, 2011

Can the class only handle 3 plist files or more?

Reply
Roman says June 30, 2011

Hi Sushant,
Thanks for this tutorial. Can you explain how to make address fully viewable. For example if the address is a little bit long it doesn’t not show the whole address but just “…”
Thanks!
Roman

Reply
L Buchan says August 24, 2011

This is one of the better explained tutorials I’ve come across for property list. However there seems to be a small problem with the coding. I’ve followed the online guide and downloaded the source code but every time i build the app the table keeps coming up empty. Any ideas where I could be going wrong?

Reply
Enrico says October 7, 2011

Great Toturial…but if I have in plist file a pdf file. How can i show it in a webView?

Reply
david says March 12, 2012

Thanks for posting this.
does anyone know how to modify the program so that the value parts of address or name or number part can show a string or make room for a real string?

example is i tried using /n in the plist but when i tried running it, it would just show only so many characters and then show ….

Reply
sridhar says June 25, 2012

Thanks! it helps a lot

Reply
naSh says July 26, 2012

Does not work with ARC?

Reply
Avant says August 3, 2012

How do you index and section a plist with that structure?

Reply
Add Your Reply