Plist文件包含两种对象: 数组和对象(字典), 由这两种文件嵌套组成.
文章内容: UITableView及解析plist文件数据的搭配使用(创建model类)
1 : RootViewController.m文件
#import "RootViewController.h"
#import "Model.h"
#import "UIColor+RandomColor.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
//全局tableView
@property (nonatomic, strong) UITableView *tableView;
//数据源数组(存放所有分组)
@property (nonatomic, strong) NSMutableArray *dataArray;
//数据源字典(单个存放每一个对象)
@property (nonatomic,strong) NSMutableDictionary *dataDic;
@end
@implementation RootViewController
static NSString *const identify = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
//初始化tableView
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
//设置代理(用来显示数据和UI对象)
self.tableView.delegate = self;
self.tableView.dataSource = self;
//添加到视图上
[self.view addSubview:self.tableView];
//注册cell(重用)
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identify];
//调用自定义解析方法
[self parserData];
}
- (void)parserData{
//获取plist文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyClass" ofType:@"plist"];
//因为plist文件的最外层数据是用字典存放的,所以将数据放到字典中
NSDictionary *dict= [NSDictionary dictionaryWithContentsOfFile:path];
//初始化数组和字典(不初始化, 会造成数据无法显示)
self.dataArray = [NSMutableArray array];
self.dataDic = [NSMutableDictionary dictionary];
//开始遍历plist文件
for (NSArray *key in dict) {
//获取字典中的每个分组(数组)
[self.dataArray addObject:key];
//初始化一个临时数组, 用来存放每次遍历出来的具体对象(暂时存放, 下面会将数据整组存放到字典中)
NSMutableArray *array = [NSMutableArray array];
//遍历每个分组中的对象
for (NSDictionary *dic in dict[key]) {
//将遍历到的对象通过model类特有的方法, 赋值给model类对象
Model *model = [Model new];
//setValuesForKeysWithDictionary 是model类特有的方法
[model setValuesForKeysWithDictionary:dic];
//将每次遍历出来的model类对象, 放到临时数组中
[array addObject:model];
}
//将临时数组中的整组对象放到全局的字典中(可以根据Key值添加)
[self.dataDic setObject:array forKey:key];
}
//因为我们获取到的字典数据是无序的, 这里使用排序选择器对数据进行排序
[self.dataArray sortUsingSelector:@selector(compare:)];
}
//根据获取到的数据设置每个分组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//找到对应分组
NSString *key = self.dataArray[section];
//找到对应分组中的人数(此时所有的对象都存放在我们的全局字典中)
NSArray *array = self.dataDic[key];
return array.count;
}
//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//根据不同的分组不同的row获取不同的数据对象
NSString *key = self.dataArray[indexPath.section];
Model *model = self.dataDic[key][indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.phoneNumber;
cell.imageView.image = [UIImage imageNamed:model.headerImg];
cell.backgroundColor = [UIColor randomColor];
return cell;
}
//设置每个分组的头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.dataArray[section];
}
//分区标题
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;
}
//快速索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.dataArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2 : Model.h文件
#import <Foundation/Foundation.h>
@interface Model : NSObject
//model类的属性, 必须和plist文件里一致(这样才能把对象整体赋值给model对象)
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *gender;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, copy) NSString *headerImg;
@end
3 : Model.m文件
#import "Model.h"
@implementation Model
//容错处理方法, 调用model对象赋值解析的数据时, 若发生属性不一致情况,则不会崩溃
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
//description方法是程序员的测试方法, 在控制台中打印出汉字
- (NSString *)description{
return [NSString stringWithFormat:@"%@", _name];
}
@end