码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发UI篇----UI基础之复杂的汽车列表

时间:2015-09-03 23:12:29      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

plist : 

技术分享

模型Car

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface CZCar : NSObject
 4 
 5 /**
 6  *  汽车名称
 7  */
 8 @property (nonatomic,copy) NSString *name;
 9 
10 /**
11  *  汽车的图标
12  */
13 @property (nonatomic,copy) NSString *icon;
14 
15 //实现字典转模型的方法
16 // 对象方法
17 - (instancetype) initWithDict:(NSDictionary *) dict;
18 
19 //类方法
20 + (instancetype) carWithDict:(NSDictionary *) dict;
21 
22 
23 @end
24 
25 
26 
27 
28 
29 #import "CZCar.h"
30 
31 @implementation CZCar
32 
33 
34 - (instancetype)initWithDict:(NSDictionary *)dict
35 {
36     if (self = [super init]) {
37         
38         self.icon = dict[@"icon"];
39         self.name = dict[@"name"];
40         
41     }
42     return self;
43 }
44 
45 + (instancetype)carWithDict:(NSDictionary *)dict
46 {
47     return [[self alloc] initWithDict:dict];
48 }
49 
50 
51 @end

模型CarGroup

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface CZCarGroup : NSObject
 4 
 5 //标题
 6 @property (nonatomic,copy) NSString *title;
 7 //汽车数组
 8 @property (nonatomic,strong) NSArray *cars;
 9 
10 //提供两个字典转模型的方法
11 //对象方法
12 - (instancetype) initWithDict:(NSDictionary *) dict;
13 //类方法
14 + (instancetype) carGroupWithDict:(NSDictionary *) dict;
15 
16 //加载plist文件,把字典数组转换为模型数组
17 + (NSArray *) carGroups;
18 
19 
20 
21 @end
22 
23 
24 
25 
26 
27 #import "CZCarGroup.h"
28 #import "CZCar.h"
29 
30 @implementation CZCarGroup
31 
32 - (instancetype)initWithDict:(NSDictionary *)dict
33 {
34     if (self = [super init]) {
35         self.title = dict[@"title"];
36         self.cars = dict[@"cars"];
37     }
38     return self;
39 }
40 
41 
42 + (instancetype)carGroupWithDict:(NSDictionary *)dict
43 {
44     return [[self alloc] initWithDict:dict];
45 }
46 
47 
48 + (NSArray *)carGroups
49 {
50 //   1.加载plist
51 //   获取绝对路径
52     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
53 //   读取数组(分组的字典)
54     NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
55     
56 //  2.把array中分组的字典转为模型
57 //  2.1 定义一个可变数组
58     NSMutableArray *arrayM = [NSMutableArray array];
59 //  遍历array,把它里面存放的组字典转换为组模型,放到arrayM中
60     for (NSDictionary *dict in array) {
61         CZCarGroup *carGroup  = [self carGroupWithDict:dict];
62 //      这个字典数组
63         NSArray *dictArray  = carGroup.cars;
64 //      把dictArray转换为一个CZCar的模型数组
65         
66 //      定义一个可变数组用来存放转换后的CZCar模型
67         NSMutableArray *carsArray = [NSMutableArray array];
68 //      遍历字典数组dictArray,把字典转换为CZCar的模型
69         for (NSDictionary *dict in dictArray) {
70 //        把字典转换为CZCar的模型
71            CZCar *car  = [CZCar carWithDict:dict];
72 //         把转换后的car添加到carsArray数组中
73             [carsArray addObject:car];
74         }
75 //     把转换后的CZCar的模型数组赋值给carGroup的cars属性
76         carGroup.cars = carsArray;
77         
78         [arrayM addObject:carGroup];
79     }
80 // 3.返回组模型数组
81     return arrayM;
82 }
83 
84 
85 @end

 控制器

#import "ViewController.h"
#import "CZCarGroup.h"
#import "CZCar.h"
@interface ViewController () <UITableViewDataSource>

//定义一个汽车分组的模型数组
@property (nonatomic,strong) NSArray *carGroups;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//  1.验证carGroups数组中存放什么东西
//    NSLog(@"%@",self.carGroups);
    
//  2.验证carGroup中的cars属性中存放是社么东西
//  取出第一个汽车分组
//    CZCarGroup *carGroup = [self.carGroups firstObject];
////  打印cars属性
//    NSLog(@"%@",carGroup.cars);
    
    
// 设置tableVeiw的数据源为控制器
    self.tableView.dataSource = self;

}

#pragma mark -  实现数据源方法
//一共多少组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carGroups.count;
}

//每一组多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//  1.取出分组对应的carGroup模型
    CZCarGroup *carGroup = self.carGroups[section];
//  2.返回cars属性的长度
    return carGroup.cars.count;
}

//每一行显示什么内容

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//  0.定义一个可重用的标示
    NSString *reuseId = @"car";
    
//  1.去缓冲池中取可重用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
//  2.如果缓冲池中没有,创建新的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
//  3.设置数据
//  3.1 取出组模型
    CZCarGroup *carGroup = self.carGroups[indexPath.section];
//  3.2 取出CZCar模型
    CZCar *car = carGroup.cars[indexPath.row];
    
//  3.3 使用car给cell设置数据
//  设置图标
    cell.imageView.image = [UIImage imageNamed:car.icon];
//  设置汽车名称
    cell.textLabel.text = car.name;
    
//  4.返回cell
    return cell;
}


// 头部标题
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//   1.取出组模型
    CZCarGroup *carGroup = self.carGroups[section];
//   2.返回组模型的标题
    return carGroup.title;
}


// 实现分组索引
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
//  存放标题
//    NSMutableArray *arrayM = [NSMutableArray array];
////  遍历carGroups,让把标题放入arrayM中
//    for (CZCarGroup *carGroup in self.carGroups) {
//        [arrayM addObject:carGroup.title];
//        
////        [arrayM addObject:@"啊哈哈"];
//    }
//    
//    return arrayM;
    
    return [self.carGroups valueForKey:@"title"];
    
    

}


#pragma mark - 隐藏状态栏

// 询问状态栏要不要隐藏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


#pragma mark - 懒加载数组
- (NSArray *)carGroups
{
    if (_carGroups == nil) {
        _carGroups = [CZCarGroup carGroups];
    }
    return _carGroups;
}
@end

 

iOS开发UI篇----UI基础之复杂的汽车列表

标签:

原文地址:http://www.cnblogs.com/DoNib-Coding/p/4780813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!