iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
一、plist文件和项目结构图

说明:这是一个嵌套模型的示例
二、代码示例:
TXCarGroup.h文件代码:
1 #import <Foundation/Foundation.h> 2 3 @interface TXCarGroup : NSObject 4 /** 5 * 这组的标题 6 */ 7 @property (nonatomic, copy) NSString *title; 8 /** 9 * 存放的所有的汽车品牌(里面装的都是MJCar模型) 10 */ 11 @property (nonatomic, strong) NSArray *cars; 12 13 + (instancetype)groupWithDict:(NSDictionary *)dict; 14 - (instancetype)initWithDict:(NSDictionary *)dict;@end
TXCarGroup.m文件代码:
1 #import "TXCarGroup.h" 2 #import "TXCar.h" 3 @implementation TXCarGroup 4 + (instancetype)groupWithDict:(NSDictionary *)dict 5 { 6 return [[self alloc] initWithDict:dict]; 7 } 8 9 - (instancetype)initWithDict:(NSDictionary *)dict 10 { 11 if (self = [super init]) { 12 // 赋值标题 13 self.title = dict[@"title"]; 14 15 // 取出原来的字典数组 16 NSArray *dictArray = dict[@"cars"]; 17 NSMutableArray *carArray = [NSMutableArray array]; 18 for (NSDictionary *dict in dictArray) { 19 TXCar *car = [TXCar carWithDict:dict]; 20 [carArray addObject:car]; 21 } 22 self.cars = carArray; 23 } 24 return self; 25 } 26 @end
TXCar.h文件
1 #import <Foundation/Foundation.h> 2 3 @interface TXCar : NSObject 4 /** 5 * 图标 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称 10 */ 11 @property (nonatomic, copy) NSString *name; 12 13 + (instancetype)carWithDict:(NSDictionary *)dict; 14 - (instancetype)initWithDict:(NSDictionary *)dict; 15 @end
TXCar.m文件
1 #import "TXCar.h" 2 3 @implementation TXCar 4 + (instancetype)carWithDict:(NSDictionary *)dict 5 { 6 return [[self alloc] initWithDict:dict]; 7 } 8 9 - (instancetype)initWithDict:(NSDictionary *)dict 10 { 11 if (self = [super init]) { 12 [self setValuesForKeysWithDictionary:dict]; 13 } 14 return self; 15 } 16 17 18 @end
YYViewController.m文件
1 #import "TXViewController.h" 2 #import "TXCar.h" 3 #import "TXCarGroup.h" 4 @interface TXViewController ()<UITableViewDataSource> 5 6 /** 7 * 车品牌组数据 8 */ 9 @property (nonatomic, strong) NSArray *groups; 10 @end 11 12 @implementation TXViewController 13 14 - (void)viewDidLoad 15 { 16 [super viewDidLoad]; 17 // Do any additional setup after loading the view, typically from a nib. 18 } 19 20 - (NSArray *)groups 21 { 22 if (_groups == nil) { 23 // 初始化 24 // 1.获得plist的全路径 25 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; 26 27 // 2.加载数组 28 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; 29 30 // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 31 NSMutableArray *groupArray = [NSMutableArray array]; 32 for (NSDictionary *dict in dictArray) { 33 // 3.1.创建模型对象 34 TXCarGroup *group = [TXCarGroup groupWithDict:dict]; 35 36 // 3.2.添加模型对象到数组中 37 [groupArray addObject:group]; 38 } 39 40 // 4.赋值 41 _groups = groupArray; 42 } 43 return _groups; 44 } 45 46 #pragma mark - 数据源方法 47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 48 { 49 return self.groups.count; 50 } 51 52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 53 { 54 TXCarGroup *group = self.groups[section]; 55 56 return group.cars.count; 57 } 58 59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 60 { 61 // 1.定义一个循环标识 62 static NSString *ID = @"car"; 63 64 // 2.从缓存池中取出可循环利用cell 65 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 66 67 // 3.缓存池中没有可循环利用的cell 68 if (cell == nil) { 69 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 70 } 71 72 // 4.设置数据 73 TXCarGroup *group = self.groups[indexPath.section]; 74 TXCar *car = group.cars[indexPath.row]; 75 76 cell.imageView.image = [UIImage imageNamed:car.icon]; 77 cell.textLabel.text = car.name; 78 79 return cell; 80 } 81 82 /** 83 * 第section组显示的头部标题 84 */ 85 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 86 { 87 TXCarGroup *group = self.groups[section]; 88 return group.title; 89 } 90 91 /** 92 * 返回右边索引条显示的字符串数据 93 */ 94 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 95 { 96 return [self.groups valueForKeyPath:@"title"]; 97 } 98 99 - (BOOL)prefersStatusBarHidden 100 { 101 return YES; 102 } 103 @end
实现效果:

三、注意点
1.设置索引
代码如下:
1 //设置索引 2 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 3 { 4 //利用kvc取出所有的标题 5 NSArray *title=[self.car valueForKeyPath:@"title"]; 6 return title; 7 }
2.cell的性能优化
代码如下:
1 static NSString *identifier=@"car"; 2 //4.1去缓存中去取cell 3 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 4 //4.2若没有,则创建cell,并盖章 5 if (cell==nil) { 6 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 7 }
浙公网安备 33010602011771号