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

我对使用属性列表有点困惑。 我已经阅读了关于这个主题的大部分其他问题,但是我仍然很困惑,因为他们只在一层,所以任何帮助,将不胜感激。

我想加载一个储存数据的plist,像这样:

在这里输入图像说明

我有三个ViewController(两个TableViewControllers和一个空白)在我的故事板,我想显示不同级别的信息:

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

如何读/写文件给plist,以及如何通过多个视图访问这些信息?

一旦我读取数据,如何显示不同TableViewControllers中的信息?

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

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)

输出iPhone模拟器图像

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

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

如果这是必要的,我使用这段代码在我的testing项目中生成示例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]; 

在阅读plist堆栈溢出和ViewControllers之间传递数据有很多例子。 build议您仔细阅读并理想地连接它们以获得您的解决scheme。

希望有所帮助。

你有这样的例子: http : //iosdevelopertips.com/data-file-management/reading-a-plist-into-an-nsarray.html

这很容易。