繁体   English   中英

将数据从plist加载到UITableView

[英]Load data from a plist to a UITableView

我正在开发一个使用Storyboard的iPhone应用程序。 第一个场景,我有一个Table View Controller。 我有一个属性文件名称公司,并且我正在尝试加载要在UITableView中显示的文件的值。 我已经待了几个小时了,但无济于事。 这个问题接近了,但是答案也没有帮助我。

这是属性文件的格式。

在此处输入图片说明

这是我的代码。

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;
}

不会填充表视图。 有人可以告诉我我所错过的一切吗?

首先,您必须分配并初始化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;
}

理想情况下,您的plist应该有一个包含一个公司键的字典,其值应该是NSArray。

您读的字典不是数组。

pList中的条目类型是什么。 您能否从您的pList中获取公司名称。 尝试通过公司的价值键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);

嗨首先,plist文件的结构必须是这样的(仅显示其中一部分)

在此处输入图片说明

然后是我放置的位置(总裁是根文件夹)

在此处输入图片说明

然后我用来读取数据的代码是(总裁是NSArray

在此处输入图片说明

加载数据后,您需要重新加载tableview。

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    [self.tableView reloadData];
}

为此,您必须创建一个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