How to use plist in iphone ?

This is the example of a plist (Property list) to get a feel for how its works.

Be Sociable, Share!

This is the example of a plist (Property list) to get a feel for how its works.

Step 1: Create a new Xcode project using View based application template.

Step 2: Double click .xib file to open the file in interface Builder.Drop a table view onto the view window. Then click connections inspector,and connect the dataSource and delegate connections to the File’s Owner icon. Next, select table view,and click attributes inspector. Change the table view’s Style to Grouped. After doing that,save,and return to Xcode.

Step 3: This project needs a fair amount of data to do its thing. So we create  a   property list and add it to Resource folder. Once it’s added to the project ,single click .plist its just look like (below figure 1).It’s a property list that contains a dictionary ,with one entry for each letter of the alphabet.

Figure 1: The sortednames.plist property list file.

Step 4: Single click the ViewController.h file and add both an NSDictionary and an NSArray instance variable and corresponding property declarations. The dictionary will hold all of our data. The array will hold the sections sorted in alphabetical order . And we also need to conform the class to the UITableViewDataSource and UITableViewDelegate protocols:

#import <UIKit/UIKit.h>

@interface SectionsViewController : UIViewController
  <UITableViewDataSource,UITableViewDelegate>
{
        NSDictionary *names;
        NSArray *keys;
}
@end

Step 5: In our tableView:cellForRowAtIndexPath:method,we have to extract both the section and row from the index path and use that to determine which value to use. And the other method, tableView:titleFoeHeaderInSection allows you to specify an optional header value for each section.

-(UITableViewCell *)tableView:(UITableView *)tableView
  cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
   NSUInteger section = [indexPath section];
   NSUInteger row = [indexPath row];

   NSString *key = [keys objectAtIndex:section];
   NSArray *nameSection = [names objectForKey:key];

   static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

   UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
  if(cell == nil)
  {
    cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
    reuseIdentifier: SectionsTableIdentifier] autorelease];

  }

  cell.text = [nameSection objectAtIndex:row];
  return cell;
 }

  -(NSString *)tableView:(UITableView *)tableView
   titleForHeaderInSection:(NSInteger)section
  {
    NSString *key = [keys objectAtIndex:section];
    return key;

  }



Figure 2: A grouped table with multiple sections.

You can downloaded SourceCode from here Sections

Be Sociable, Share!
WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

14 Responses to “How to use plist in iphone ?”

  1. Raj says:

    Sorry. I’m a newbie. I can’t see the code where the dictionary or array loads the data from the plist. Which line is that?

  2. Josh Highland says:

    your zip file is missing the plist file. could you please email it to me?

  3. Siddhpura Dharmesh R. says:

    I want to know about how to edit plist which the user created as shown in the above example(plist containing of A-Z keys)? So please can you help me for that..

  4. Sushant says:

    If you want to edit pList, just open the pList and double click on the content (whatever you want to display) and change the name of the content.

  5. Alex says:

    this is exactly what i was trying to do however im not able to get the search bar to work. could you please help me??

  6. IHSAN KHAN says:

    Any one who needs a plist which is missing in this project.
    just email to ihsancse@gmail.com and request for plist then i will send it to u.

  7. IHSAN KHAN says:

    Any one who needs a plist which is missing in this project.
    just email me i will send it.

  8. IHSAN says:

    Any one who needs plist wich is missing in this project then contact me through ihsancse@gmail.com

  9. Sushant says:

    I have uploaded new Source Code, Now you can download the pList from the Source Code.

  10. RHB says:

    I have been lloking for days for an example of how to use a plist. My array is 254 x 19 (array of arrays)

    Can you explain how to put the sample files into xcode to run.

    THX

  11. dhineshbabu says:

    thanks for ur program now i can under stand the plist program correctly . thanks a lot yar

  12. roberto says:

    can you email me the source code and plist file for this project? Thanks a lot

  13. suresh says:

    cn u xplain plist concept in ios

  14. Mina says:

    This is the kind of search I need. But the source code doesn’t seem to work. I’m running it in an iPhone simulator. Everything works but the search.

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG