您的位置:首页 > 其它

双模型 -- 汽车品牌

2015-12-08 13:46 302 查看
//  FLCar.h

#import <Foundation/Foundation.h>

@interface FLCar : NSObject

// 汽车名称

@property (nonatomic,copy)NSString *name;

// 汽车图标

@property (nonatomic,copy)NSString *icon;

+ (instancetype)carWithDict:(NSDictionary *)dict;

@end

//  FLCar.m

#import "FLCar.h"

@implementation FLCar

// 字典转模型

+ (instancetype)carWithDict:(NSDictionary *)dict {

    //
创建模型对象

    id obj = [[selfalloc]init];

    // kvc转模型

    [obj setValuesForKeysWithDictionary:dict];

    return obj;

}

@end

//  FLCarGroup.h

// 每一组所有的汽车

#import <Foundation/Foundation.h>

@interface FLCarGroup :
NSObject

/**

 *  组的头部标题

 */

@property (nonatomic,copy)NSString *title;

/**

 *  当前组所有汽车(装的是每一个汽车对象)

 */

@property (nonatomic,strong)NSArray *cars;

+ (instancetype)carGroupWithDict:(NSDictionary *)dict;

@end

// FLCarGroup.m

#import "FLCarGroup.h"

#import "FLCar.h"

@implementation FLCarGroup

// 汽车组的字典转模型方法

+ (instancetype)carGroupWithDict:(NSDictionary *)dict {

    //
创建模型对象

  FLCarGroup *cg = [[selfalloc]init];

//    [obj setValuesForKeysWithDictionary:dict];

    //
设置标题属性

    cg.title = dict[@"title"];

   
// 取出字典中的数组cars数组中当前装的是字典

    NSArray *dictArr = dict[@"cars"];

   
// 创建可变数组,并组数组一个初始的容量

    NSMutableArray *arrM = [NSMutableArrayarrayWithCapacity:dictArr.count];//单单array也可以

    

   
// 遍历数组字典转模型对象

    for (NSDictionary *dictin dictArr) {

        //
字典转换成模型对象

        FLCar *car = [FLCarcarWithDict:dict];

        //
添加到可变数组

        [arrM addObject:car];

    }

    //
把car模型数组赋值给cars这个数组属性

    cg.cars = arrM;

    

    return cg;

}

@end

//  ViewController.m

#import "ViewController.h"

#import "FLCarGroup.h"

#import "FLCar.h"

@interface
ViewController ()<UITableViewDataSource>

@property (nonatomic,strong)NSArray *groupes;

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    

}

// 一共有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

   
// 模型数组中的汽车组模型个数就代表我们有多少组(一个汽车组模型代表一组)

    return
self.groupes.count;

}

// 每一组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   
// 先取出汽车组模型

    FLCarGroup *cg =
self.groupes[section];

   
// 组模型中的汽车数组个数就表示有多少行

    return cg.cars.count;

}

// 返回每一组的每一行显示什么样的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1.定义重用标识符

    static NSString *ID =@"car";

    // 2.先去缓存池找可重用的cell

    UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:ID];

   
// 3.如果缓存池中没有可重用的cell.创建一个新的cell,并组新创建的cell绑定重用标识

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
b891

    }

   
// 1.取出模型数组中的,汽车组模型

    FLCarGroup *cg =
self.groupes[indexPath.section];

   
// 2.取出汽车组模型中的汽车模型

    FLCar *car = cg.cars[indexPath.row];

    

    //
设置cell的图片

    cell.imageView.image = [UIImageimageNamed:car.icon];

    //
设置cell的主标题

    cell.textLabel.text = car.name;

    

    //
返回cell

    return cell;

}

// 返回每一组的头部标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    //
取出第section的汽车组模型

    FLCarGroup *cg =
self.groupes[section];

   
// 返回每组的头部标题

    return cg.title;

}

// 返回索引数组

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

   
// 间接的去取数组中所有的title属性的值,装入数组,最后返回数组

    return [self.groupesvalueForKeyPath:@"title"];

}

// 隐藏状态栏

- (BOOL)prefersStatusBarHidden {

    return
YES;

}

#pragma mark - 懒加载  

- (NSArray *)groupes {

    if (_groupes ==nil)
{  // 当数组为空的时候再来加载数组

        //
获取plist文件路径

        NSString *path = [[NSBundlemainBundle]pathForResource:@"cars_total.plist"ofType:nil];

        

        //
读取plist

        NSArray *dictArr = [NSArrayarrayWithContentsOfFile:path];

       
// 创建可变数组,并组数组一个初始容量

        NSMutableArray *arrM = [NSMutableArrayarrayWithCapacity:dictArr.count];

        //
遍历数组字典转模型

        for (NSDictionary *dictin dictArr) {

           //转换出来的汽车组模型,内部会再把cars这个字典数组转成cars模型数组

            FLCarGroup *cg = [FLCarGroupcarGroupWithDict:dict];

            //
把汽车组模型添加到可变数组

            [arrM addObject:cg];

        }

        // 给数组赋值

        _groupes = arrM;

    }

    return_groupes;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: