Tutorial details

Populate a UITableView From a plist  | App Code for Sale | Preview

Populate a UITableView From a plist | iOS Tutorial

Populate a UITableView From a plist (property list)

Overview PAGE TOP

Start a new “single view” project (I named it PopulateUITableView). Delete ViewController.h and ViewController.m from the project navigator (command+1). Open MainStoryboard.storyboard and delete the scene that came with the template. In the object library find a Table View Controller and drag it as a new scene. This is what the project should look like at this point.

UITableView1.jpg

To manage the table view we need to create a new class TableViewController that is a s subclass of UITableViewController. (command+N) create a new Objective-C class name it TableViewController and make sure that it is a subclass of UITableViewController.

UITableView2.jpg

Go back to MainStoryboard.storyboard. Select the Table View Controller, go to the identity inspector (option+command+3) and change the custom class to TableViewController, the class we just created.

UITableView3.jpg

lick on the Table View Cell and go to Attributes Inspector (option+command+4) and change the style of the cell from basic to subtitle.It is time to create the plist file.

Create a new file (command+N) under iOS pick Resource and then select Property List. Name the file Data. This will create a Data.plist file. Open Data.plist and change the type of the root key to array and add three new items under root by clicking on the plus sign. Change their type to dictionary and for each item add two new sub-items of type string. One string should have the name city and the other state for each item. Give value for each city to New York, Chicago, and Los Angeles and their respective state names. This is what Data.plist should look like after you are done.

UITableView4.jpg

Next, we need to pull the data from the plist and load it into an array. To do that we declare a NSArray property in TableViewController.h

@interface TableViewController : UITableViewController
@property (strong, nonatomic) NSArray *content;
@end

Open TableViewController.m Synthesize the property and write a getter that initializes the array with the content of the plist file

@synthesize content = _content;

-(NSArray *)content
{
    if (!_content) {
        _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
    }
    return _content;
}

Finally, we have to implement the two required table view data source methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the 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:@"city"]; cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"]; return cell; }

Run the app (command+R) and the table will be loaded from the plist file.

Reference PAGE TOP

How to Populate a UITableView From a plist (property list) from http://ios-blog.co.uk

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

80x15.png

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.