从字典和数组的列表中读取/写入数据,并将不同级别加载到TableView中 [英] Read/write data from a plist of dictionaries and arrays, and load different levels into a TableView

查看:186
本文介绍了从字典和数组的列表中读取/写入数据,并将不同级别加载到TableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用属性列表有些困惑.我已经阅读了有关该主题的大多数其他问题,但由于它们仅在一层中,因此我仍然感到非常困惑,因此我们将不胜感激.

I'm a little confused about working with property lists. I've read most of the other questions about this topic, but I'm still very confused since they only go in one layer, so any help would be appreciated.

我想加载一个存储如下数据的plist:

I would like to load a plist that stores data somewhat like this:

我的情节提要中有三个ViewController(两个TableViewControllers和一个空白),我想显示不同级别的信息:

I have three ViewControllers (two TableViewControllers and one blank) in my Storyboard that I want to display different levels of information:

  1. 人的类别(朋友等)
  2. 这些类别(鲍勃等)中的人的名字
  3. 有关人的信息(喜欢的颜色等)

如何将文件读/写到plist,以及如何跨多个视图访问该信息?

How do I read/write files to a plist, and how to I access that information across multiple views?

读取数据后,如何在不同的TableViewControllers中显示信息?

Once I read the data, how do I display the information in different TableViewControllers?

推荐答案

假设您有一个这样的plist文件:

Let's say that you have a plist file like this:

和此代码:

@implementation ViewController


NSDictionary *dictionary;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *array=[[NSMutableArray alloc]init];
    array=[NSMutableArray arrayWithContentsOfFile:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"root.plist"]];

    dictionary = [array objectAtIndex:0];


}

- (NSString *)applicationDocumentsDirectory {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"numberOfSectionsInTableView:%lu",(unsigned long)dictionary.count);
    return dictionary.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[[dictionary allKeys] objectAtIndex:section] capitalizedString];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSArray *peopleArray = [dictionary objectForKey:[[dictionary allKeys] objectAtIndex:indexPath.section]];

    cell.textLabel.text = [[peopleArray objectAtIndex:indexPath.row]objectForKey:@"name"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Color:%@ - Gender:%@",[[peopleArray objectAtIndex:indexPath.row]objectForKey:@"favorite color"],[[peopleArray objectAtIndex:indexPath.row]objectForKey:@"gender"]];

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key =[[dictionary allKeys] objectAtIndex:section];
    NSLog(@"numberOfRowsInSection:%lu",(unsigned long)[[dictionary objectForKey:key] count]);
    return [[dictionary objectForKey:key] count];
}

它将为您提供以下输出:

it will give you this output:

(假设您已使用Delegate和DataSource设置了tableView)

(Supposed you have a tableView set up with Delegate and DataSource)

我知道您要求在不同的TableView中使用朋友"和敌人",但是我在同一个tableView中但在不同的部分中都使用了它们.但是我的代码可以帮助您入门.

I know that you asked for having the "Friends" and "Enemies" in different TableViews, but I use both in the same tableView but in different sections. But my code can get you started.

如果您需要有关UITableView的其他帮助,请参阅Apple

If you need extra help about the UITableView, see Apple UITableView Class Reference

如果有必要,我可以使用以下代码在我的测试项目中生成示例plist文件:

If this is necessary, I used this code to generate the example plist-file in my test-project:

NSMutableArray *root=[[NSMutableArray alloc]init];
NSMutableArray *friendsarray = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"Jill" forKey:@"name"];
[dict setObject:@"green" forKey:@"favorite color"];
[dict setObject:@"female" forKey:@"gender"];

[friendsarray addObject:dict];

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
[dict2 setObject:@"Bob" forKey:@"name"];
[dict2 setObject:@"Blue" forKey:@"favorite color"];
[dict2 setObject:@"male" forKey:@"gender"];

[friendsarray addObject:dict2];

NSMutableArray *enemiesarray = [[NSMutableArray alloc]init];


NSMutableDictionary *dict3 = [[NSMutableDictionary alloc]init];
[dict3 setObject:@"Michael" forKey:@"name"];
[dict3 setObject:@"Red" forKey:@"favorite color"];
[dict3 setObject:@"male" forKey:@"gender"];

[enemiesarray addObject:dict3];

NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
[d setObject:friendsarray forKey:@"friends"];
[d setObject:enemiesarray forKey:@"enemies"];


[root addObject:d];

[root writeToFile:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"root.plist"] atomically:YES];

这篇关于从字典和数组的列表中读取/写入数据,并将不同级别加载到TableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆