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;

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 Reply

Your email address will not be published. Required fields are marked *