A.实现思路
1.拖入UITableView
2.拖曳、连线UITableView控件
3.Controller遵守UITalbeViewDataSource协议
4.设置UITableView的dataSource
5.加载数据到Model
6.从Model解析数据,显示到View上
 
 
 
B.实现细节
1.UITableView style
(1)Grouped,成组出现,标题和尾部会被分隔开,如上图
 
(2)Plain
 
 
 

2. cars_simple.plist 文件结构

 
C.主要代码
Car.h
 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
 
Car.m
 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
 
ViewController.m
 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多项显示的更多相关文章

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

    A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代 ...

  2. [iOS基础控件 - 6.2] LOL英雄列表 UITableView单项显示

    A.需求 1.使用只有一个section的TableView来显示LOL 的英雄列表 2.内容包括标题.副标题.图标 3.使用plain样式 4.使用MVC模式     heros.plist 文件结 ...

  3. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  4. [iOS基础控件 - 6.7.1] 微博展示 代码

      Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...

  5. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  6. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  7. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  8. [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo

    A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用   B.实现步骤 1.准备plist文件和国旗图片     2.创建模型 // // Flag.h // Co ...

  9. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

随机推荐

  1. 【无聊放个模板系列】BZOJ 1597 斜率优化

    STL 双向队列DEQUE版本 #include<cstdio> #include<cstdlib> #include<cstring> #include<i ...

  2. ArcGIS学习记录—属性表的编辑与修改

    原文地址: ArcGIS问题:属性表的编辑与修改 - Silent Dawn的日志 - 网易博客 http://gisman.blog.163.com/blog/static/344933882009 ...

  3. JS选中OPTION

    var obj_prov = document.getElementById("prov"); var prov_text = obj_prov.options[obj_prov. ...

  4. (转)CAP理论十二年回顾:"规则"变了

    编者按:由InfoQ主办的全球架构师峰会将于2012年8月10日-12日在深圳举行,为了更好地诠释架构的意义.方法和实践,InfoQ中文站近期会集中发布一批与架构相关的文章,本篇即为其中之一.Info ...

  5. 【HDOJ】4579 Random Walk

    1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...

  6. poj2186Popular Cows(强连通分量)

    http://poj.org/problem?id=2186 用tarjan算出强连通分量的个数 将其缩点 连成一棵树  则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根 #includ ...

  7. 函数buf_LRU_block_remove_hashed_page

    /******************************************************************//** Takes a block out of the LRU ...

  8. bzoj3170

    以前写的,好像忘写解题报告 注意是一个跟曼哈顿距离很有用的结论 |xi-xj|+|yi-yj|=max(|xi+yi-(xj+yj)|,|xi-yi+(xj-yj)|) 因为绝对值有个性质是|a-b| ...

  9. jquery M97-datepicker日历控件

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.在页面中引入该组件js文件:     <script type=&quo ...

  10. c#中单元测试

    从走进.net后发现每天有写不完的代码,有做不完的测试...人感觉都已经机械,我们需要认清自己调整好心态,问下自己是否真的喜欢编程.我的答案当然也就是我爱编码,编码给我带来了许多欢乐,每天都给我体验小 ...