简体   繁体   中英

Load data from a plist to a UITableView

I'm developing a iPhone application which is using Storyboard. The first scene, I have a Table View Controller. I have a properties file names companies and I'm trying to load the Values of that file to be displayed in the UITableView. I've been at this for hours but to no avail. This question came close but it's answers didn't help me either.

Here is the format of the Properties file.

在此处输入图片说明

Here is my code.

CompaniesTableViewController.m file

#import "CompaniesTableViewController.h"

@interface CompaniesTableViewController ()

@end

@implementation CompaniesTableViewController

NSMutableArray *companies;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
    companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [companies count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];

    return cell;
}

The Table View does not get populated. Can anybody tell me what I'm overlooking, missing here?

First of all you have to allocate and init the UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }
    cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];

    return cell;
}

Ideally uor plist should be having a dictionary with one key as companies and its value should be an NSArray.

You read a dictionary not an array.

What are the entry types in your pList. Can you get the names of companies from your pList at all. Try NSLogging the companies names via the companies' value key.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"companies" ofType:@"plist"];
    NSData *plistData = [NSData dataWithContentsOfFile:path];
    NSString *error; NSPropertyListFormat format;
    NSArray *companyData = [NSPropertyListSerialization propertyListFromData:plistData
                                                                      mutabilityOption:NSPropertyListImmutable
                                                                                format:&format
                                                                      errorDescription:&error];

  companies= [[NSMutableArray alloc] initWithArray:companyData];
    NSLog(@"companyData:%@",companyData);

Hi First of all the structure of the plist file is must be like this (only a part of it is shown)

在此处输入图片说明

Then the place where I put it is (president is the root folder)

在此处输入图片说明

Then the code I used to read data is (where the presidents is NSArray )

在此处输入图片说明

After loading the data you need to reload the tableview.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
    companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];

    [self.tableView reloadData];
}

for that kind of plist.. You must create a NSDictonary..

@implementation CompaniesTableViewController

    {
    NSDictionary *dict;
    NSArray *arayDict;
    }

..

NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
//populate dicationary
dict = [[NSDictionary alloc] initWithContentsOfFile:companiesFile];
//populate array with allkeys from dict
arayDict = [dict allKeys];

--

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayDict count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *key = [arayDict objectAtIndex:indexPath.row];
    NSDictionary *dictionary = [dict objectForKey:key];
    cell.textLabel.text =[NSString stringWithFormat:@"%@", dictionary];
    return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM