先看效果:數組
這裏的數據模型有兩層:每一組汽車是一層模型,每一組裏面的每一行汽車品牌也是一層模型。緩存
在
WSCars.h
中:性能優化
#import <Foundation/Foundation.h> @interface WSCars : NSObject @property(nonatomic,copy) NSString *icon; @property(nonatomic,copy) NSString *name; +(WSCars *)carsWithDict:(NSDictionary *)dict; -(WSCars *)initWithDict:(NSDictionary *)dict; @end
在
WSCars.m
中:性能
#import "WSCars.h" @implementation WSCars +(WSCars *)carsWithDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict]; } -(WSCars *)initWithDict:(NSDictionary *)dict{ if ([super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end
在
WSCarGroup.h
中:優化
#import <Foundation/Foundation.h> @interface WSCarGroup : NSObject @property(nonatomic,copy) NSString * title; @property(nonatomic,strong) NSArray *cars; +(WSCarGroup *)carGroupWithDict:(NSDictionary *)dict; -(WSCarGroup *)initWithDict:(NSDictionary *)dict; @end
在
WSCarGroup.m
中:(此處作了1次字典轉模型,即把每一個汽車數據轉成WSCars對象)ui
#import "WSCarGroup.h" #import "WSCars.h" @implementation WSCarGroup +(WSCarGroup *)carGroupWithDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict]; } -(WSCarGroup *)initWithDict:(NSDictionary *)dict{ if ([super init]) { self.title=dict[@"title"]; NSArray *dictArray=dict[@"cars"]; NSMutableArray *muArray=[[NSMutableArray alloc]init]; for (NSDictionary * dic in dictArray) { WSCars *car=[[WSCars alloc]initWithDict:dic]; [muArray addObject:car]; } self.cars=muArray; } return self; } @end
ViewController.m
中,定義數組,而且把字典轉模型@property (nonatomic,strong) NSArray *carsArray;
//字典轉模型 - (NSArray *)carsArray{ if (_carsArray==nil) { NSString *path=[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil]; NSArray *totalArray=[NSArray arrayWithContentsOfFile:path]; NSMutableArray *muArray=[[NSMutableArray alloc]init]; for (NSDictionary *dict in totalArray) { WSCarGroup *carGroup=[[WSCarGroup alloc]initWithDict:dict]; [muArray addObject:carGroup]; } _carsArray=muArray; } return _carsArray; }
數組工做至此完成。atom
@interface ViewController ()<UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *tableView; @end
- (void)viewDidLoad { //設置數據源 self.tableView.dataSource=self; //設置行高 self.tableView.rowHeight=60; [super viewDidLoad]; }
//設置多少組 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carsArray.count; } //設置多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ WSCarGroup *carGroup=self.carsArray[section]; return carGroup.cars.count; } //設置cell內容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //先緩存池,性能優化 static NSString *ID=@"car"; UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } //取出數據 WSCarGroup *carGroup=self.carsArray[indexPath.section]; WSCars *cars=carGroup.cars[indexPath.row]; //賦值給cell cell.textLabel.text=cars.name; cell.imageView.image=[UIImage imageNamed:cars.icon]; return cell; } //設置組名 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ WSCarGroup *carGroup=self.carsArray[section]; return carGroup.title; }
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ //須要返回一個數組 //用valueForKey只能在本層級字典中查找,而self.carsArray是數組,且沒有title關鍵字 //用valueForKeyPath能夠在本級及下級字典數組中查找,有path路徑 return [self.carsArray valueForKeyPath:@"title"]; }
//隱藏狀態欄 -(BOOL)prefersStatusBarHidden{ return YES; }
總結:spa
——難度在於字典轉模型的地方,由於模型有2層級。
——增長了一個知識點,即顯示組索引。用sectionIndexTitlesForTableView
方法,返回值是一個數組,因此咱們這裏也用到了valueForKeyPath
這個方法取得一個字符串組。.net
轉至:小小的客棧——大大的江湖code