[iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示




2. cars_simple.plist 文件结构
1 //
2 // Car.h
3 // 01-CarBrand
4 //
5 // Created by hellovoidworld on 14/11/30.
6 // Copyright (c) 2014年 hellovoidworld. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 @interface Car : NSObject
12
13 @property(nonatomic, strong) NSArray *cars;
14 @property(nonatomic, copy) NSString *title;
15 @property(nonatomic, copy) NSString *desc;
16
17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
18 + (instancetype) carWithDictionary:(NSDictionary *) dictionary;
19 + (instancetype) car;
20
21 @end
1 //
2 // Car.m
3 // 01-CarBrand
4 //
5 // Created by hellovoidworld on 14/11/30.
6 // Copyright (c) 2014年 hellovoidworld. All rights reserved.
7 //
8
9 #import "Car.h"
10
11 @implementation Car
12
13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
14 if (self == [super init]) {
15 self.cars = dictionary[@"cars"];
16 self.title = dictionary[@"title"];
17 self.desc = dictionary[@"desc"];
18 }
19
20 return self;
21 }
22
23 + (instancetype) carWithDictionary:(NSDictionary *) dictionary {
24 return [[self alloc] initWithDictionary:dictionary];
25 }
26
27 + (instancetype) car {
28 return [self carWithDictionary:nil];
29 }
30
31 @end
1 //
2 // ViewController.m
3 // 01-CarBrand
4 //
5 // Created by hellovoidworld on 14/11/30.
6 // Copyright (c) 2014年 hellovoidworld. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import "Car.h"
11
12 @interface ViewController () <UITableViewDataSource>
13
14 @property (weak, nonatomic) IBOutlet UITableView *tableView;
15
16 @property(nonatomic, strong) NSArray *allBrandOfCars;
17
18 @end
19
20 @implementation ViewController
21
22 - (void)viewDidLoad {
23 [super viewDidLoad];
24 // Do any additional setup after loading the view, typically from a nib.
25
26 self.tableView.dataSource = self;
27 }
28
29 - (void)didReceiveMemoryWarning {
30 [super didReceiveMemoryWarning];
31 // Dispose of any resources that can be recreated.
32 }
33
34
35 #pragma mark - dataSource方法
36
37 /** Sections 数,组数 */
38 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
39 return self.allBrandOfCars.count; // 所有车的派系的数量
40 }
41
42 /** 组内的行数 */
43 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
44 Car *car = self.allBrandOfCars[section];
45 return car.cars.count; // 每个派系的车的牌子的数量
46 }
47
48 /** 组的标题
49 这里是车的派系
50 */
51 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
52 Car *car = self.allBrandOfCars[section];
53 return car.title;
54 }
55
56 /** 组的尾部 */
57 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
58 Car *car = self.allBrandOfCars[section];
59 return car.desc;
60 }
61
62 /** 行的内容 */
63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
64 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
65
66 Car *car = self.allBrandOfCars[indexPath.section]; // 车的派系
67 NSString *carName = car.cars[indexPath.row]; // 具体车的牌子
68
69 cell.textLabel.text = carName;
70
71 return cell;
72 }
73
74
75 /** 延迟加载plist文件中的数据 */
76 - (NSArray *) allBrandOfCars {
77 if (nil == _allBrandOfCars) {
78 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_simple" ofType:@"plist"]];
79
80 NSMutableArray *mdictArray = [NSMutableArray array];
81 for (NSDictionary *dict in dictArray) {
82 Car *car = [Car carWithDictionary:dict];
83 [mdictArray addObject:car];
84 }
85
86 _allBrandOfCars = mdictArray;
87 }
88
89 return _allBrandOfCars;
90 }
91
92
93 @end
[iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示的更多相关文章
- [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引
A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代 ...
- [iOS基础控件 - 6.2] LOL英雄列表 UITableView单项显示
A.需求 1.使用只有一个section的TableView来显示LOL 的英雄列表 2.内容包括标题.副标题.图标 3.使用plain样式 4.使用MVC模式 heros.plist 文件结 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [iOS基础控件 - 6.7.1] 微博展示 代码
Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
- [iOS基础控件 - 7.0] UIWebView
A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的 2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用 B.实现步骤 1.准备plist文件和国旗图片 2.创建模型 // // Flag.h // Co ...
- [iOS基础控件 - 6.9] 聊天界面Demo
A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...
随机推荐
- 探讨read的返回值的三种情况
http://blog.chinaunix.net/uid-23629988-id-3035613.html 今天探讨一个很看似简单的API “read”的返回值问题.read的返回值有哪几个值?每个 ...
- Qt: 把内容写进字符串中与C++很相似(使用QTextStream包装QString)
#include <iostream>#include <QChar>#include <QFile>#include <QTextStream>#in ...
- Kali2.0 Sqlmap清除历史扫描日志
使用Sqlmap扫描SQL注入漏洞时,首次扫描会在SQL的/root/.sqlmap/output/目录下留下 以IP地址为名称的文件夹,如下所示: 而如果该安全漏洞经过修复后,再次使用SQLMAP扫 ...
- VS下遇到未能加载文件或程序集 错误
这个的错误原因可能是在64的系统上编译32位的应用程序,遇到这个错误,可以通过下面的手段解决! 1.关闭Visual Studio. 2. 在Visual Studio Tools子目录,以管理员身份 ...
- #error作用与举例
2013-09-05 14:32:17 #error命令是C/C++语言的预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息. 语法: #error [用 ...
- unite
列出某个集合里的项目,比如file,buffer等 :United file——列出文件 :United buffer——列出buffer :United file_rec——递归列出文件 进入Uni ...
- javascript里的post和get有什么区别
FORM中的get post方法区别Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法.二者主要区别如下: 1.Get是用来从服务器上获得数据,而Post ...
- 【HDOJ】5296 Annoying problem
LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] ...
- [原]Unity3D深入浅出 - 认识开发环境中的Component(组件)菜单
Component(组件)是用来添加到GameObject对象上的一组相关属性,本质上每个组件都是一个类的实例,比如在Cube上添加一个Mesh网格,即面向对象的思维方式可以理解成Cube对象里包含了 ...
- 5个难以置信的VS 2015预览版新特性
Visual Studio 2015 Preview包含了很多强大的新特性,无论你是从事WEB应用程序开发,还是桌面应用程序开发,甚至是移动应用开发,VS 2015都将大大提高你的开发效率.有几个特性 ...