UITableView 读取Plist文件

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,303评论 1 14
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,951评论 3 38
  • 下面是我最近两年学习OC中的一些基础知识,对于学习OC基础知识的人可能有些帮助,拿出来分享一下,还是那句话不喜勿喷...
    小小赵纸农阅读 7,530评论 1 7
  • 课程要点:plist文件的新建与读取给UITableView设置变化的值单元格的删除、插入及刷新 plist...
    shiwuoo阅读 7,495评论 0 6
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,312评论 30 472