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

【iOS开发之旅】汽车品牌展示

时间:2016-01-25 22:41:44      阅读:862      评论:0      收藏:0      [点我收藏+]

标签:

运行效果如下:

技术分享  技术分享 

技术分享技术分享
模型:

//
//  CZGroup.h
//  05-汽车品牌展示
//
//  Created by ChenQianPing on 16/1/22.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CZGroup : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *cars;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)groupWithDict:(NSDictionary *)dict;


@end
//
//  CZGroup.m
//  05-汽车品牌展示
//
//  Created by ChenQianPing on 16/1/22.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "CZGroup.h"
#import "CZCar.h"

@implementation CZGroup
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]){
        [self setValuesForKeysWithDictionary:dict];
        
        // 当有模型嵌套的时候需要手工把字典转成模型
        // 创建一个用来保存模型的数组
        NSMutableArray *arrayModels = [NSMutableArray array];
        // 手工作一下字典转模型
        for (NSDictionary *item_dict in dict[@"cars"]) {
            CZCar *model = [CZCar carWithDict:item_dict];
            [arrayModels addObject:model];
        }
        self.cars = arrayModels;
    }
    return self;
}

+ (instancetype)groupWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
@end
//
//  CZCar.h
//  05-汽车品牌展示
//
//  Created by ChenQianPing on 16/1/22.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CZCar : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end
//
//  CZCar.m
//  05-汽车品牌展示
//
//  Created by ChenQianPing on 16/1/22.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "CZCar.h"

@implementation CZCar
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]){
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)carWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
@end

控制器:

//
//  ViewController.m
//  05-汽车品牌展示
//
//  Created by ChenQianPing on 16/1/22.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "ViewController.h"
#import "CZGroup.h"
#import "CZCar.h"

@interface ViewController () <UITableViewDataSource>
// 用来保存所有的组信息
@property (nonatomic,strong) NSArray *groups;


@end

@implementation ViewController

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

// 返回每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CZGroup *group = self.groups[section];
    // 返回每一组有多少辆车
    return group.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.获取模型数据
    // 根据组的索引获取对应的组的模型
    CZGroup *group = self.groups[indexPath.section];
    // 根据当前行的索引,获取对应组的对应行的车
    CZCar *car = group.cars[indexPath.row];
    
    // 2.创建单元格
    // 2.1 声明一个重用ID
    static NSString *ID = @"car_cell";
    // 2.2 根据重用ID去缓存池中获取对应的cell对象
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.3 如果没有获取到,那么创建一个
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 3.设置单元格内容
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    // 4.返回单元格
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 获取组模型
    CZGroup *group = self.groups[section];
    return group.title;
}

// 设置UITableView右侧的索引栏
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//    NSMutableArray *arrayIndex = [NSMutableArray array];
//    for (CZGroup *group in self.groups)
//    {
//        [arrayIndex addObject:group.title];
//    }
//    return arrayIndex;
    return [self.groups valueForKey:@"title"];
}


#pragma mark - 懒加载数据
// 一般的数据,在从文件或数据库中读取后,会用到多次,这时候要养成懒加载数据的习惯,以提高程序的性能,其实你不使用懒加载,从使用者来说,如果数据量小,没什么影响,但如果数据量大,使用懒加载数据就有明显的优势了。
- (NSArray *)groups{
    if (_groups == nil) {
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
        // 2.加载数组
        NSArray *arraryDict = [NSArray arrayWithContentsOfFile:path];
        // 3.将arraryDict里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *arrayModels = [NSMutableArray array];
        for (NSDictionary *dict in arraryDict) {
            // 3.1.创建模型对象
            CZGroup *model = [CZGroup groupWithDict:dict];
            // 3.2.添加模型对象到数组中
            [arrayModels addObject:model];
        }
        // 4.赋值
        _groups = arrayModels;
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 隐藏顶部状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

 



【iOS开发之旅】汽车品牌展示

标签:

原文地址:http://www.cnblogs.com/Bobby0322/p/5158814.html

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