##先看效果 ##具體實現 ###一、準備資料 圖片資源和plist文件(源碼中包含) ###二、創建模型api
//JCar.h文件 @interface JCar : NSObject /** * 圖標 */ @property (nonatomic,copy) NSString *icon; /** * 名稱 */ @property (nonatomic,copy) NSString *name; + (instancetype)carWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end //JCar.m文件 @implementation JCar + (instancetype)carWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end
// JCarGroup.h文件 @interface JCarGroup : NSObject /** * 組標題 */ @property (nonatomic,copy) NSString *title; /** * 該組下全部的Car */ @property (nonatomic,strong) NSArray *cars; + (instancetype)groupWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end // JCarGroup.m文件 #import "JCarGroup.h" #import "JCar.h" @implementation JCarGroup + (instancetype)groupWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { // 賦值組名稱 self.title = dict[@"title"]; // 組中的汽車列表 NSArray *dictArray = dict[@"cars"]; NSMutableArray *carArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { JCar *car = [JCar carWithDict:dict]; [carArray addObject:car]; } self.cars = carArray; } return self; } @end
###三、實現效果數組
// // ViewController.m // 01-汽車品牌 // // Created by yshye on 15/12/15. // Copyright © 2015年 yshye. All rights reserved. // #import "ViewController.h" #import "JCar.h" #import "JCarGroup.h" @interface ViewController () <UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableview; /** * 車品牌數組 */ @property (nonatomic,strong) NSArray *carGroups; @end @implementation ViewController - (NSArray *)carGroups{ if (_carGroups == nil) { // 初始化 // 1.得到plist全路徑 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"]; // 2.加載數組 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.將dictArray裏面的全部字典轉成模型對象,放在新的數組中 NSMutableArray *groupCar = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1 建立模型對象 JCarGroup *group = [JCarGroup groupWithDict:dict]; // 3.2 添加模型對象到數組中 [groupCar addObject:group]; } // 4.賦值 _carGroups = groupCar; } return _carGroups; } - (void)viewDidLoad { [super viewDidLoad]; self.tableview.dataSource = self; self.tableview.delegate = self; } #pragma mark - 數據源方法 /** * 一共有多少數據,不寫時,默認是一組 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carGroups.count; } /** * 第section組有多少行 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ JCarGroup *cg = self.carGroups[section]; NSArray * cars =cg.cars; return cars.count; } /** * 每一行顯示怎麼樣的內容(call) */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.定義一個循環標示 static NSString *ID = @"car"; // 2.從緩存池中取出可循環利用的cell UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; // 3.緩存池中沒有可循環利用的call if (cell == nil) { cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 4.設置數據 JCarGroup *cg = self.carGroups[indexPath.section]; JCar *car =cg.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:car.icon]; cell.textLabel.text =car.name; return cell; } /** * 設置第section組顯示的頭部標題 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ JCarGroup *cg = self.carGroups[section]; return cg.title; } /** * 設置第section組顯示的尾部標題 */ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ // JCarGroup *cg = self.carGroups[section]; return @""; } /** * 設置右側導航索引 * * @param tableView <#tableView description#> * * @return <#return value description#> */ - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.carGroups valueForKeyPath:@"title"]; } - (BOOL)prefersStatusBarHidden{ return YES; } #pragma mark - 代理方法 ///** // * 數據顯示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // return [super ta]; //} ///** // * 頭部數據顯示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ // return 30; //} // ///** // * 尾部數據顯示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ // return 30; //} @end
##源碼 01-汽車品牌.zip緩存