设置tableviewView组的快速检索
2016-04-24 17:17
309 查看
@interface
ViewController ()<UITableViewDataSource>
@property (nonatomic,
strong) NSArray *dataArray;
@property (nonatomic,
strong) NSArray *indexArray;//检索的索引数组
@end
@implementation ViewController
#pragma mark -
#pragma mark - 懒加载
- (NSArray *)dataArray {
if (nil ==
_dataArray) {
// 1. 路径
NSString *path = [[NSBundle
mainBundle] pathForResource:@"cars_total.plist"
ofType:nil];
// 2. 读取
NSArray *tempArray = [NSArray
arrayWithContentsOfFile:path];
// 3.
临时可变数组
NSMutableArray *mutal = [NSMutableArray
array];
// 4. 转
for (NSDictionary *dict
in tempArray) {
CarModel *model = [CarModel
carModelWithDict: dict];
[mutal addObject:model];
}
//
把carModel中title
放入数组,
用作后面的索引返回值
_indexArray = [mutal
valueForKeyPath:@"title"];
_dataArray = mutal;
}
return
_dataArray;
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return
self.dataArray.count;
}
// 多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CarModel *carModel =
self.dataArray[section];
return carModel.cars.count;
}
// 每一行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1.
定义一个重用标识符
static NSString *identifier =
@"settingCell";
// 2. 到缓存池中去找对应的cell,
根据重用标识符
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identifier];
// 3. 判断是否找到,
如果找不到, 就重新实例化cell
if (nil == cell) {
cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
// 4.
设置数据
//
先取出该组对应的model
CarModel *carModel =
self.dataArray[indexPath.section];
//
再取出该组中行对应的model
InnerCarModel *innerCarModel = carModel.cars[indexPath.row];
//
对cell的控件进行赋值
cell.imageView.image = [UIImage
imageNamed:innerCarModel.icon];
cell.textLabel.text = innerCarModel.name;
return cell;
}
//设置组头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//
取出carModel
CarModel *carModel =
self.dataArray[section];
return carModel.title;
}
//设置组的快速搜索
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return
_indexArray;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden {
return
YES;
}
ViewController ()<UITableViewDataSource>
@property (nonatomic,
strong) NSArray *dataArray;
@property (nonatomic,
strong) NSArray *indexArray;//检索的索引数组
@end
@implementation ViewController
#pragma mark -
#pragma mark - 懒加载
- (NSArray *)dataArray {
if (nil ==
_dataArray) {
// 1. 路径
NSString *path = [[NSBundle
mainBundle] pathForResource:@"cars_total.plist"
ofType:nil];
// 2. 读取
NSArray *tempArray = [NSArray
arrayWithContentsOfFile:path];
// 3.
临时可变数组
NSMutableArray *mutal = [NSMutableArray
array];
// 4. 转
for (NSDictionary *dict
in tempArray) {
CarModel *model = [CarModel
carModelWithDict: dict];
[mutal addObject:model];
}
//
把carModel中title
放入数组,
用作后面的索引返回值
_indexArray = [mutal
valueForKeyPath:@"title"];
_dataArray = mutal;
}
return
_dataArray;
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return
self.dataArray.count;
}
// 多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CarModel *carModel =
self.dataArray[section];
return carModel.cars.count;
}
// 每一行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1.
定义一个重用标识符
static NSString *identifier =
@"settingCell";
// 2. 到缓存池中去找对应的cell,
根据重用标识符
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identifier];
// 3. 判断是否找到,
如果找不到, 就重新实例化cell
if (nil == cell) {
cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
// 4.
设置数据
//
先取出该组对应的model
CarModel *carModel =
self.dataArray[indexPath.section];
//
再取出该组中行对应的model
InnerCarModel *innerCarModel = carModel.cars[indexPath.row];
//
对cell的控件进行赋值
cell.imageView.image = [UIImage
imageNamed:innerCarModel.icon];
cell.textLabel.text = innerCarModel.name;
return cell;
}
//设置组头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//
取出carModel
CarModel *carModel =
self.dataArray[section];
return carModel.title;
}
//设置组的快速搜索
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return
_indexArray;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden {
return
YES;
}
相关文章推荐
- JAVA集合框架之set
- 隐藏文件的好方法
- 特征值分解、奇异值分解、PCA概念整理
- 第 20 章 项目实战--资讯内容[5,6]
- 第一冲刺阶段站立会议08
- 根据计算机名获取其IP
- 归来
- 剑指offer--用两个栈实现队列
- jQuery 学习一:选择器
- Gradle学习系列之一——Gradle快速入门
- IOS设计模式代理模式
- Android For JNI(六)——交叉编译,NDK概述以及文件结构,编写自己的第一个JNI工程
- Android For JNI(六)——交叉编译,NDK概述以及文件结构,编写自己的第一个JNI工程
- iOS开发实战:如何将非ARC的项目转换成ARC项目
- Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作
- php错误:访问网页出现Forbidden you donnot have permission to access … on this server
- linux学习总结
- 简单对比C#程序中的单线程与多线程设计
- POJ 2253 Frogger(dijkstra变形)
- 使用GNU make 编译C程序