复杂模型分组展示

#import "HMViewController.h"
#import "HMCarGroup.h"
#import "HMCar.h" @interface HMViewController () <UITableViewDataSource>
@property (nonatomic, strong) NSArray *carGroups;
@property (nonatomic, strong) UITableView *tableView;
@end @implementation HMViewController - (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self; [self.view addSubview:_tableView];
}
return _tableView;
} - (NSArray *)carGroups
{
if (_carGroups == nil) {
_carGroups = [HMCarGroup carGroups];
}
return _carGroups;
} - (void)viewDidLoad
{
[super viewDidLoad]; // 调用tableView添加到视图
[self tableView];
} #pragma mark - 数据源方法
// 分组总数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
} // 每一组的总数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
HMCarGroup *group = self.carGroups[section]; return group.cars.count;
} // 单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 可重用标示符
static NSString *ID = @"Cell"; // 让表格缓冲区查找可重用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 如果没有找到可重用cell
if (cell == nil) {
// 实例化cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 设置cell内容
// 1> 取出数据模型
HMCarGroup *group = self.carGroups[indexPath.section];
HMCar *car = group.cars[indexPath.row]; // 2> 设置数据
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name; return cell;
} // 标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 找到group
HMCarGroup *group = self.carGroups[section]; return group.title;
} // 右侧索引列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// 索引数组中的"内容",跟分组无关
// 索引数组中的下标,对应的是分组的下标
// return @[@"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello"]; // 返回self.carGroup中title的数组
// NSMutableArray *arrayM = [NSMutableArray array];
// for (HMCarGroup *group in self.carGroups) {
// [arrayM addObject:group.title];
// }
// return arrayM; // KVC是cocoa的大招
// 用来间接获取或者修改对象属性的方式
// 使用KVC在获取数值时,如果指定对象不包含keyPath的"键名",会自动进入对象的内部查找
// 如果取值的对象是一个数组,同样返回一个数组
NSArray *array = [self.carGroups valueForKeyPath:@"cars.name"];
NSLog(@"%@", array); return [self.carGroups valueForKeyPath:@"title"];
} @end

IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)的更多相关文章

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

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

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

    A.实现思路 1.拖入UITableView 2.拖曳.连线UITableView控件 3.Controller遵守UITalbeViewDataSource协议 4.设置UITableView的da ...

  3. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  4. IOS第七天(3:UiTableView 模型和数据的分组的显示)

    *************UiTableView模型和数据的分组的显示 #import "HMViewController.h" #import "HMHero.h&qu ...

  5. IOS第七天(2:UiTableView 加上数据分离)

    ****加上数据分离 #import "HMViewController.h" #import "HMStudent.h" @interface HMViewC ...

  6. IOS第七天(1:UiTableView 的基本用法)

    ***表格控件 #import "HMViewController.h" @interface HMViewController () <UITableViewDataSou ...

  7. IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)

    //加上头部 和底部 - (void)viewDidLoad { [super viewDidLoad]; [self tableView]; // 设置行高 self.tableView.rowHe ...

  8. iOS UI-表格控制器(UITableView)-基本使用

    tableView的常见属性 cell的常见属性 一.一般情况 #import "ViewController.h" @interface ViewController ()< ...

  9. (十八)TableView实践(多组汽车品牌展示)

    对于多组数据,可能会用到模型的嵌套. 例如多组汽车,每组是一个模型,组内有多辆车的信息,每辆车的信息也是一个模型,相当于模型中有模型. 可以看到,每个item是一个字典,这要创建一个模型,而模型内部的 ...

随机推荐

  1. UVA 1314 最小表示法

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36117 题意:给定长度为n的字符串,求一个起点使字符串从该起点起的 ...

  2. iOS10 UI教程视图的边界与视图的框架

    iOS10 UI教程视图的边界与视图的框架 iOS10 UI视图的边界 在视图的几何形状中我们提到了视图属性中的一部分属性可以将定义的视图绘制在屏幕上.其中典型的3个属性为边界属性.框架属性以及中心位 ...

  3. Swift3.0语言教程字符串与文件的数据转换

    Swift3.0语言教程字符串与文件的数据转换 Swift3.0语言教程字符串与文件的数据转换,如果想要对字符串中的字符进行永久保存,可以将字符串中的字符写入到文件中.当然,开发者也可以将写入的内容进 ...

  4. 黑客语(Leet)

    黑客语(Leet)   Leet是从网络发展起来的一种文字书写方式.通常将英语中的字母替换为数字和特殊符号.这种方式被很多黑客组织所使用.由于具有隐密性,所以它也广泛被用于密码中.使用Leet书写的密 ...

  5. ajax 异步交互

    <script>     $(function(){         $("#sub").click(function () {             $.ajax( ...

  6. Python基础4- 字符串

    Python字符串是由数字.字母.下划线组成的一串字符,我们可以使用引号来创建字符串.如:str = 'Helloworld'在Python中没有char类型,单个字符也作为string使用; Pyt ...

  7. 关于Ue4的深度模板

    http://www.unrealchina.net/forum.php?mod=viewthread&tid=100234

  8. 424 - Integer Inquiry

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...

  9. JS类的封装及实现代码

    js并不是一种面向对向的语言, 没有提供对类的支持, 因此我们不能像在传统的语言里那样 用class来定义类, 但我们可以利用js的闭包封装机制来实现js类, 我们来封装一个简的Shape类. 1. ...

  10. 【HDU】4089 Activation

    http://acm.hdu.edu.cn/showproblem.php?pid=4089 题意: 有n个人排队等着在官网上激活游戏.主角排在第m个. 对于队列中的第一个人.有以下情况:1.激活失败 ...