本文示範如何以「嵌套模型」的方式,把分組資料與下層項目組織起來,並用 UITableView 呈現汽車圖標清單。重點在於分組模型內部如何把子項目的字典陣列轉成模型陣列。
#import@interface YYcarsgroup : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, strong) NSArray *cars; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)carsgroupWithDict:(NSDictionary *)dict; @end
#import "YYcarsgroup.h"
#import "YYcars.h"
@implementation YYcarsgroup
-(instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
// 嵌套字典轉模型
self.title = dict[@"title"];
NSArray *dictcars = dict[@"cars"];
// 預先設定容量可提升效能
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictcars.count];
for (NSDictionary *dict in dictcars) {
YYcars *yycars = [[YYcars alloc] initWithDict:dict];
[arrayM addObject:yycars];
}
// 把存模型的陣列賦值給屬性
self.cars = arrayM;
}
return self;
}
+(instancetype)carsgroupWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end#import@interface YYcars : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *icon; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)carsWithDict:(NSDictionary *)dict; @end
#import "YYcars.h"
@implementation YYcars
-(instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+(instancetype)carsWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end#import "YYViewController.h" #import "YYcarsgroup.h" #import "YYcars.h" @interface YYViewController ()@property (strong, nonatomic) IBOutlet UITableView *tableview; @property (nonatomic, strong) NSArray *car; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableview.rowHeight = 60.f; self.tableview.dataSource = self; NSLog(@"%d", self.car.count); } #pragma mark - 懶加載 // 1. 從 bundle 讀取資料 2. 字典轉模型 3. 回傳 cars -(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) { YYcarsgroup *carsgroup = [YYcarsgroup carsgroupWithDict:dict]; [carsarray addObject:carsgroup]; } _car = [carsarray copy]; } return _car; } #pragma mark - 實現 tableview 數據展示 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.car.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { YYcarsgroup *carsgroup = self.car[section]; return carsgroup.cars.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"car"; // 4.1 從緩存取 cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 4.2 若無則創建 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 4.3 設置數據 YYcarsgroup *carsgroup = self.car[indexPath.section]; YYcars *yycars = carsgroup.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:yycars.icon]; cell.textLabel.text = yycars.name; // 4.4 回傳 cell return cell; } // 設置每組標題 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { YYcarsgroup *carsgroup = self.car[section]; return carsgroup.title; } // 設置索引 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { // 利用 KVC 取出所有標題 NSArray *title = [self.car valueForKeyPath:@"title"]; return title; } // 隱藏狀態欄 -(BOOL)prefersStatusBarHidden { return YES; } @end
透過嵌套模型,分組與其下的項目被清晰地串成樹狀,再配合 KVC 快速取出所有分組標題作為索引,是這個範例的兩個亮點。