做列表頁時,資料往往不是扁平的一維陣列,而是帶有分組的巢狀結構。這時,「字典轉模型」的技巧就派上用場了。本文以一個汽車展示程式為例,示範如何用嵌套模型優雅地處理這類資料,並透過 UITableView 分組展示。
資料分成兩層:外層是「汽車品牌分組」(YYcarsgroup),內層是「具體車款」(YYcars)。先來看車款模型:
#import <Foundation/Foundation.h>
@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 <Foundation/Foundation.h>
@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
在 ViewController 中,我們透過懶加載把 plist 讀進來並轉成模型,再交給 UITableView 分組呈現:
#import "YYViewController.h"
#import "YYcarsgroup.h"
#import "YYcars.h"
@interface YYViewController ()<UITableViewDataSource>
@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";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// 依 section 與 row 取出對應資料
YYcarsgroup *carsgroup = self.car[indexPath.section];
YYcars *yycars = carsgroup.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:yycars.icon];
cell.textLabel.text = yycars.name;
return cell;
}
// 每組的標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
YYcarsgroup *carsgroup = self.car[section];
return carsgroup.title;
}
// 右側索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// 利用 KVC 取出所有標題
return [self.car valueForKeyPath:@"title"];
}
// 隱藏狀態列
-(BOOL)prefersStatusBarHidden {
return YES;
}
@end
這個範例的精髓在於「字典轉模型」的分層處理:外層模型負責管理分組標題,內層模型負責車款細節。透過巢狀結構,複雜資料被拆解得井井有條,列表渲染自然流暢。掌握了這套思路,你就能應付絕大多數需要分組展示的清單場景。