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

时间:2014-02-24 03:57:33

标签: ios uitableview plist

我对使用属性列表感到有些困惑。我已经阅读了关于这个主题的大多数其他问题,但我仍然非常困惑,因为他们只进入一层,所以任何帮助都会受到赞赏。

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

enter image description here

我的Storyboard中有三个ViewControllers(两个TableViewControllers和一个空白),我想显示不同级别的信息:

  1. 人物类别(朋友等)
  2. 这些类别中的人的姓名(Bob等)
  3. 有关人物的信息(喜欢的颜色等)
  4. 如何读取/写入plist文件,以及如何跨多个视图访问该信息?

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

3 个答案:

答案 0 :(得分:1)

reading plistpassing data between ViewControllers的Stack Overflow中有很多示例。建议您仔细阅读它们。理想情况下将它们连接在一起以获得解决方案。

希望有所帮助。

答案 1 :(得分:1)

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

plist file image

和这段代码:

@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)

Output iPhone Simulator image

我知道您要求在不同的TableView中使用“Friends”和“Enemies”,但我在同一个tableView中使用它们但在不同的部分中。但是我的代码可以让你开始。

如果您需要有关UITableView的额外帮助,请参阅Apple UITableView Class Reference

如果有必要,我使用此代码在我的测试项目中生成示例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];

答案 2 :(得分:0)