从字典和数组的 plist 中读取/写入数据,并将不同级别加载到 TableView 中

2024-04-06

我对使用属性列表有点困惑。我已经阅读了有关该主题的大多数其他问题,但我仍然很困惑,因为它们只进入一层,因此任何帮助将不胜感激。

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

我的故事板中有三个视图控制器(两个 TableView 控制器和一个空白),我想显示不同级别的信息:

  1. 人员类别(朋友等)
  2. 这些类别中人员的姓名(Bob 等)
  3. 有关人物的信息(最喜欢的颜色等)

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

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


假设您有一个如下所示的 plist 文件:

和这段代码:

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

它会给你这个输出:

(假设你有一个带有Delegate和DataSource的tableView)

我知道您要求在不同的 TableView 中拥有“朋友”和“敌人”,但我在同一个 tableView 中但在不同的部分中使用两者。但我的代码可以帮助您入门。

如果您需要有关 UITableView 的额外帮助,请参阅 AppleUITableView类参考 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html

如果这是必要的,我使用此代码在我的测试项目中生成示例 plist 文件:

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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从字典和数组的 plist 中读取/写入数据,并将不同级别加载到 TableView 中 的相关文章

随机推荐