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

运行效果如下:

  


模型:

//
//  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
时间: 2024-08-25 12:28:48

【iOS开发之旅】汽车品牌展示的相关文章

iOS基础控件之 汽车品牌展示 Model嵌套/KVC/TableView索引

A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代替遍历取出所有索引值) B.实现 1.Model嵌套 其实就是将另一个Model作为成员 .plist 文件结构 GroupCar中有存储Car的数组 1 @property(nonatomic, strong) NSArray *cars; 封装Model的时候需要进行相应处理 CarGroup.

[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引

A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代替遍历取出所有索引值) B.实现 1.Model嵌套 其实就是将另一个Model作为成员 .plist 文件结构 GroupCar中有存储Car的数组 1 @property(nonatomic, strong) NSArray *cars; 封装Model的时候需要进行相应处理 CarGroup.

iOS开发之旅之UIViewController解析

就iOS开发来说,UIViewController就最核心的类型之一.而iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名就可以看出它在MVC中所扮演的角色,那就是Controller啦. Controller作为整个UI视图的控制器,对于用户的输入做出逻辑处理,例如用户点击某个按钮应该执行什么操作等:View角色只负责显示视图,view的这部分就是我们在nib或者storyboard设计的UI了.Model也就是我们的数据模型,例如从Core data中加

设计模式之单一职责原则(iOS开发,代码用Objective-C展示)

单一职责原则:就一个类而言,应该只有一个引起它变化的原因. 在iOS开发中,我们会很自然的给一个类添加各种各样的功能,比如随便写一个简单的应用程序,一般都会生成一个viewController类,于是我们将各种各样的代码,商业运算的算法.http请求的参数(params)封装.使用FMDB.coreData时的数据库访问语句都放在这个类里面,这就意味着,无论任何需求变化,都要来修改viewController这个类,这其实是很糟糕的,维护麻烦.复用不可能.缺乏灵活性. 也许上面说的略微夸张,因为

大钟的ios开发之旅(2)————简单说说ios中ARC与非ARC模式下的property的变量修饰词

/******************************************************************************************** * author:[email protected]大钟 * E-mail:[email protected] *site:http://www.idealpwr.com/ *深圳市动力思维科技发展有限公司 * http://blog.csdn.net/conowen * 注:本文为原创,仅作为学习交流使用,转

大钟的ios开发之旅(4)————简单谈谈ios程序界面实现的三种方式(代码创建,xib和storyboard)

/******************************************************************************************** * author:[email protected]大钟 * E-mail:[email protected] *site:http://www.idealpwr.com/ *深圳市动力思维科技发展有限公司 * http://blog.csdn.net/conowen * 注:本文为原创,仅作为学习交流使用,转

设计模式之工厂模式(iOS开发,代码用Objective-C展示)

<大话设计模式>这是一本经典之作,本来我该看<Objective-C编程之道:IOS设计模式解析 >,其实我也是先看的<Objective-C编程之道:IOS设计模式解析 >,但不得不说,其中内容有些深奥,理解起来比较困难.这与我一贯的学习方针不合,我更喜欢一个循序渐进的过程,从认知到实践再到思维上的一个比较深入的学习.然后有朋友向我推荐了<大话设计模式>这本书籍,初看,感觉很是符合我现在这个阶段,在此以前,我所编码中接触的设计模式都是比较简单的,在代码上也

【iOS开发之旅】UITableView入门示例

运行效果:                    ViewController.h // // ViewController.h // 02-UITableView示例 // // Created by ChenQianPing on 16/1/21. // Copyright © 2016年 chenqp. All rights reserved. // #import <UIKit/UIKit.h> //在ViewController头文件中,让ViewController实现两个协议 /

【iOS开发之旅】第一个iOS程序

启动界面:开发环境版本:模拟器运行效果:main.m // // main.m // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import <UIKit/UIKit.h> #import "AppDelegate.h" // IOS程序是从main开始运行的 int main(int argc, char