#import <Foundation/Foundation.h>
@interface LHLCar : NSObject
/**
* 圖標
*/
@property (nonatomic, copy)NSString *icon;
/**
* 車名
*/
@property (nonatomic, copy)NSString *name;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end
#import "LHLCar.h"
@implementation LHLCar
+ (instancetype)carWithDict:(NSDictionary *)dict{
LHLCar *car = [[self alloc] init];
// car.icon = dict[@"icon"];
// car.name = dict[@"name"];
[car setValuesForKeysWithDictionary:dict];
return car;
}
@end
#import <Foundation/Foundation.h>
@interface LHLCarGroups : NSObject
/**
* 車模型
*/
@property (nonatomic, strong) NSArray *cars;
/**
* 標題 ABCD...
*/
@property (nonatomic, copy) NSString *title;
+ (instancetype)groupsWithDict:(NSDictionary *)dict;
@end
#import "LHLCarGroups.h"
#import "LHLCar.h"
@implementation LHLCarGroups
+ (instancetype)groupsWithDict:(NSDictionary *)dict{
// 創建一個LHLCarGroups對象,用于接收dict傳來的值
LHLCarGroups *groups = [[self alloc] init];
// 設置title
groups.title = dict[@"title"];
// 字典數組 -> 模型數組
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *carDict in dict[@"cars"]) {
[arrayM addObject:[LHLCar carWithDict:carDict]];
}
groups.cars = arrayM;
return groups;
}
@end
//
// ViewController.m
// 20-索引條
//
// Created by admin on 16/8/27.
// Copyright ? 2016年 admin. All rights reserved.
//
#import "ViewController.h"
#import "LHLCar.h"
#import "LHLCarGroups.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation ViewController
/**
* 懶加載
*/
- (NSArray *)carGroups{
if (_carGroups == nil) {
// 加載plist文件
NSArray *carDict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil]];
// 字典模型 -> 數組模型
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *groupsDict in carDict) {
[arrayM addObject:[LHLCarGroups groupsWithDict:groupsDict]];
}
_carGroups = arrayM;
}
return _carGroups;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 設置索引條文字顏色
self.tableView.sectionIndexColor = [UIColor redColor];
// 設置索引條背景顏色
self.tableView.sectionIndexBackgroundColor = [UIColor grayColor];
}
/**
* 隱藏狀態欄
*/
- (BOOL) prefersStatusBarHidden{
return YES;
}
#pragma mark - 數據源方法
/**
* 有多少個分組
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
}
/**
* 每一組有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
LHLCarGroups *groups = self.carGroups[section];
return groups.cars.count;
}
/**
* 單元格
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"Car";
// 在緩存池中通過ID查找可重用的單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果沒有找到 則創建一個cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 設置數據
// NSLog(@"%@",_carGroups);
LHLCarGroups *groups = self.carGroups[indexPath.section];
LHLCar *car = groups.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
/**
* 設置表格的title
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
LHLCarGroups *groups = self.carGroups[section];
return groups.title;
}
/**
* 索引條
*/
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
// NSMutableArray *arrayM = [NSMutableArray array];
// for (LHLCarGroups *groups in self.carGroups) {
// [arrayM addObject:groups.title];
// }
// return arrayM;
// KVC取值
return [self.carGroups valueForKeyPath:@"title"];
}
@end
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。