假设您有一个如下所示的 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];