我对使用属性列表感到有些困惑。我已经阅读了关于这个主题的大多数其他问题,但我仍然非常困惑,因为他们只进入一层,所以任何帮助都会受到赞赏。
我想加载一个存储数据的plist:
我的Storyboard中有三个ViewControllers(两个TableViewControllers和一个空白),我想显示不同级别的信息:
如何读取/写入plist文件,以及如何跨多个视图访问该信息?
读取数据后,如何在不同的TableViewControllers中显示信息?
答案 0 :(得分:1)
reading plist和passing data between ViewControllers的Stack Overflow中有很多示例。建议您仔细阅读它们。理想情况下将它们连接在一起以获得解决方案。
希望有所帮助。
答案 1 :(得分:1)
假设你有一个像这样的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中使用“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)