@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 = [[NSBundlemainBundle] pathForResource:@"cars_total.plist"ofType:nil];
// 2. 读取
NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];
// 3. 临时可变数组
NSMutableArray *mutal = [NSMutableArrayarray];
// 4. 转
for (NSDictionary *dictin tempArray) {
CarModel *model = [CarModelcarModelWithDict: dict];
[mutal addObject:model];
}
// 把carModel中title放入数组, 用作后面的索引返回值
_indexArray = [mutal valueForKeyPath:@"title"];
_dataArray = mutal;
}
return_dataArray;
}
- (void)viewDidLoad {
[superviewDidLoad];
// 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 = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
// 4. 设置数据
// 先取出该组对应的model
CarModel *carModel = self.dataArray[indexPath.section];
// 再取出该组中行对应的model
InnerCarModel *innerCarModel = carModel.cars[indexPath.row];
// 对cell的控件进行赋值
cell.imageView.image = [UIImageimageNamed: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;
}