简体   繁体   English

将数据从plist加载到UITableView

[英]Load data from a plist to a UITableView

I'm developing a iPhone application which is using Storyboard. 我正在开发一个使用Storyboard的iPhone应用程序。 The first scene, I have a Table View Controller. 第一个场景,我有一个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. 我有一个属性文件名称公司,并且我正在尝试加载要在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 CompaniesTableViewController.m文件

#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

- (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. 理想情况下,您的plist应该有一个包含一个公司键的字典,其值应该是NSArray。

You read a dictionary not an array. 您读的字典不是数组。

What are the entry types in your pList. pList中的条目类型是什么。 Can you get the names of companies from your pList at all. 您能否从您的pList中获取公司名称。 Try NSLogging the companies names via the companies' value key. 尝试通过公司的价值键NSLogging公司名称。

    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) 嗨首先,plist文件的结构必须是这样的(仅显示其中一部分)

在此处输入图片说明

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 ) 然后我用来读取数据的代码是(总裁是NSArray

在此处输入图片说明

After loading the data you need to reload the tableview. 加载数据后,您需要重新加载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.. 为此,您必须创建一个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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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