iphone开发之表格组件UITableView的使用(六)通过加载plist文件展示汽车品牌并添加索引查找

本文主要是介绍iphone开发之表格组件UITableView的使用(六)通过加载plist文件展示汽车品牌并添加索引查找,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、
*使用cars_total.plist
*模型嵌套模型
**注意:这里使用的是嵌套模型,所以不能直接使用KVC了。需要通过把字典转模型的代码封装到Group模型中。
二、
*实现右侧的索引栏
*通过实现数据源协议-(NSArray *)sectionIndexTitlesForTabView:(UITableView *)tableView
*点击右侧索引栏中的文字,会根据索引的顺序跳转到左侧对应的位置
*获取右侧group数组中每个对象的title值,并返回到一个NSArray中。[self.groups  valueForKeyPath:@“title”];
三、注意:
一个包含对象的数组array,调用valueForKeyPath方法后会把指定键的所有值放到一个新的数组中。 例如:
NSArray  *arr = [array valueForKeyPath:@“name”];
 ***因为array本身没有name属性,所以以上方法调用,会返回array这个数组中的每个元素对象的name属性的值,会把这些name放到一个新的NSArray中返回。******

代码验证示例如下:plist文件如下:


新建工程,

编辑Car.h如下:

//
//  CZCar.h
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import <Foundation/Foundation.h>@interface CZCar : NSObject
@property (nonatomic, strong) NSString * icon;
@property (nonatomic, strong) NSString *name;-(instancetype)initWithDict:(NSDictionary *) dict;
+(instancetype ) carWithDict: (NSDictionary *) dict;
@end
编辑CZCar.m如下:

//
//  CZCar.m
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import "CZCar.h"@implementation CZCar
-(instancetype)initWithDict:(NSDictionary *) dict
{if (self = [super  init]) {
//        self.name = dict[@"name"];
//        self.icon = dict[@"icon"];// 因为dict字典对象的数据元素都是基本类型,可以直接用KVC[self setValuesForKeysWithDictionary:dict];}return self;
}
+(instancetype ) carWithDict: (NSDictionary *) dict
{return [[self alloc] initWithDict:dict];
}
@end
编辑CZGroup.h如下:

//
//  CZGroup.h
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import <Foundation/Foundation.h>
#import "CZCar.h"
@interface CZGroup : NSObject
@property (nonatomic, strong) NSArray * cars;
@property (nonatomic, strong) NSString *title;-(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) groupWithDict:(NSDictionary *)dict;@end
编辑CZGroup.m如下:

//
//  CZGroup.m
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import "CZGroup.h"@implementation CZGroup
-(instancetype) initWithDict:(NSDictionary *)dict
{if (self = [super init]) {
//        self.name = dict[@"title"];
//        self.cars = dict[@"cars"];  // 因为cars是个字典,要进行模型装换[self setValuesForKeysWithDictionary:dict];  // 相当于给普通数据赋值// 当有模型嵌套的时候需要手动把字典转为模型// 创建一个用来保存模型的数组NSMutableArray *arrayModels = [NSMutableArray array];// 手动做一下模型转换for(NSDictionary * item_dict  in  dict[@"cars"]) // cars也是一个字典数组{ // 注意:声明的迭代项目不能和后面的集合名相同CZCar *car = [CZCar carWithDict:item_dict];[arrayModels addObject:car];}self.cars = arrayModels;}return self;
}
+(instancetype) groupWithDict:(NSDictionary *)dict
{return [[self alloc ]initWithDict:dict];
}@end

编辑控制器的.h文件:

//
//  ViewController.h
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import <UIKit/UIKit.h>
#import "CZGroup.h"
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray * groups;  // 保存所有的数据
@property (nonatomic, strong) UITableView *tableView; 
@end
编辑控制器的.m如下:

//
//  ViewController.m
//  通过plist文件展现汽车品牌
//
//  Created by apple on 15/9/1.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import "ViewController.h"
#define  WIDTH      [UIScreen mainScreen].bounds.size.width
#define   HEIGHT  [UIScreen mainScreen].bounds.size.height@interface ViewController ()@end@implementation ViewController// 重写groups的get方法实现懒加载
-(NSArray *)groups
{if (_groups == nil) {// 在app安装的根目录下获取指定plist文件的路径NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];// 读取文件内容到一个数组NSArray * arrayDicts = [NSArray arrayWithContentsOfFile:path];// 创建一个保存模型对象的可变数组NSMutableArray *arrayModels = [NSMutableArray array];// 遍历字典数组,将字典转为对象模型并存入存放模型对象的可变数组for(NSDictionary *dict  in arrayDicts){// 新建model对象CZGroup *group = [CZGroup  groupWithDict:dict];[arrayModels addObject:group];}_groups = arrayModels;}return _groups;
}- (void)viewDidLoad
{[super viewDidLoad];//为UItableView组建设置属性self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];self.tableView.dataSource = self;// 当每一行高度相同的情况下,可用以下属性设置self.tableView.rowHeight = 60;[self.view addSubview:self.tableView];}// 利用代理中的方法取消状态栏
-(BOOL) prefersStatusBarHidden
{return  YES;
}// 分为多少组
-(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 声明一个重用IDstatic NSString *ID = @"Car_Cell";// 2.2  根据重用ID去缓存池中获取对应的CellUITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];// 2.3 如果没有获取到,那么就创建一个具有重用ID的单元格if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];}// 设置单元格内容cell.imageView.image = [UIImage imageNamed:car.icon];cell.textLabel.text = car.name;return cell;
}// 添加数据源协议中的方法,实现添加表头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{// 获取组模型CZGroup *group = self.groups[section];return  group.title;
}// 利用代理中的方法添加右侧索引标题栏
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//    NSMutableArray *arrayIndex = [NSMutableArray array];
//    for(CZGroup *group in self.groups)
//    {
//        [arrayIndex addObject:group.title];
//    }
//    return arrayIndex;
//    return [self.groups valueForKeyPath:@"title"];
}
- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

运行结果如下:







这篇关于iphone开发之表格组件UITableView的使用(六)通过加载plist文件展示汽车品牌并添加索引查找的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115291

相关文章

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected