說明: title
屬性表示該 item
下汽車名字的首字母, cars
屬性存放首字母爲 title
的汽車, icon
屬性存放圖片的名稱, name
屬性存放汽車的名字。ide
2、代碼實例性能
新建一個繼承自 NSObject
的類,命名爲 WJQCars
,該類用於存放首字母相同的汽車。atom
// WJQCars.h @interface WJQCars : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *icon; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)carsWithDict:(NSDictionary *)dict; @end
// WJQCars.m @implementation WJQCars - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { self.name = dict[@"name"]; self.icon = dict[@"icon"]; } return self; } + (instancetype)carsWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; }
新建一個繼承自 NSObject
的類,並命名爲 WJQCarsGroup
,該類用於存放不一樣的 WJQCars
。spa
// WJQCarsGroup.h @interface WJQCarsGroup : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, strong) NSArray *cars; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)carsGroupWithDict:(NSDictionary *)dict; @end
在 WJQCarsGroup.m
中導入 WJQCars.h
,實現文件代碼以下:code
// WJQCarsGroup.m - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { self.title = dict[@"title"]; NSArray *dictCars = dict[@"cars"]; // 有助於提升性能 NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictCars.count]; for (NSDictionary *dict in dictCars) { WJQCars *wcars = [[WJQCars alloc] initWithDict:dict]; [arrayM addObject:wcars]; } self.cars = arrayM; } return self; } + (instancetype)carsGroupWithDict:(NSDictionary *)dict { return [[self alloc]initWithDict:dict]; }
在 ViewController.m
中導入 WJQCarsGroup.h、WJQCars.h
。繼承
// ViewController.m @interface ViewController () <UITableViewDataSource> @property (nonatomic, strong) IBOutlet UITableView *tableView; @property (nonatomic, strong) NSArray *car; @end 將 tableView 屬性與 Main.storyboard 中拖入的 Table View 視圖創建鏈接。並讓 ViewController 遵照 UITableViewDataSource 協議。 // ViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.tableView.rowHeight = 60; self.tableView.dataSource = self; NSLog(@"self.car.count = %d", self.car.count); } // 從包中讀取數據,實現字典轉模型 - (NSArray *)car { if (_car == nil) { NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; NSArray *arrayM = [NSArray arrayWithContentsOfFile:fullPath]; NSMutableArray *carsArray = [NSMutableArray array]; for (NSDictionary *dict in arrayM) { WJQCarsGroup *carsGroup = [WJQCarsGroup carsGroupWithDict:dict]; [carsArray addObject:carsGroup]; } _car = [carsArray copy]; } return _car; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.car.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { WJQCarsGroup *carsGroup = self.car[section]; return carsGroup.cars.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"car"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } WJQCarsGroup *carsGroup = self.car[indexPath.section]; WJQCars *cars = carsGroup.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:cars.icon]; cell.textLabel.text = cars.name; return cell; } // 設置每組的標題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { WJQCarsGroup *carsGroup = self.car[section]; return carsGroup.title; } // 設置首字母索引 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSArray *title = [self.car valueForKeyPath:@"title"]; // 使用KVC機制 return title; }