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

相关文章

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进

Java中使用注解校验手机号格式的详细指南

《Java中使用注解校验手机号格式的详细指南》在现代的Web应用开发中,数据校验是一个非常重要的环节,本文将详细介绍如何在Java中使用注解对手机号格式进行校验,感兴趣的小伙伴可以了解下... 目录1. 引言2. 数据校验的重要性3. Java中的数据校验框架4. 使用注解校验手机号格式4.1 @NotBl

Python使用DeepSeek进行联网搜索功能详解

《Python使用DeepSeek进行联网搜索功能详解》Python作为一种非常流行的编程语言,结合DeepSeek这一高性能的深度学习工具包,可以方便地处理各种深度学习任务,本文将介绍一下如何使用P... 目录一、环境准备与依赖安装二、DeepSeek简介三、联网搜索与数据集准备四、实践示例:图像分类1.

Linux系统之authconfig命令的使用解读

《Linux系统之authconfig命令的使用解读》authconfig是一个用于配置Linux系统身份验证和账户管理设置的命令行工具,主要用于RedHat系列的Linux发行版,它提供了一系列选项... 目录linux authconfig命令的使用基本语法常用选项示例总结Linux authconfi

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win