[iOS基础控件-6.4]汽车品牌展示Model嵌套/KVC/TableView索引

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

A.需求

1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model

2.使用KVC进行Model封装赋值

3.展示头字母标题

4.展示索引(使用KVC代替遍历取出所有索引值)

B.实现

1.Model嵌套

其实就是将另一个Model作为成员

.plist 文件结构

GroupCar中有存储Car的数组

1@PRoperty(nonatomic, strong) NSArray *cars;

封装Model的时候需要进行相应处理

CarGroup.m

1//这里不能用KVC,封装car model再放到array中,不能直接存储array2NSArray *carsArray = dictionary[@"cars"];3NSMutableArray *mcarsArray =[NSMutableArray array];4for(NSDictionary *dictincarsArray) {5Car *car =[Car carWithDictionary:dict];6[mcarsArray addObject:car];7}89self.cars = mcarsArray;

2.KVC

成员名使用和从dictionary取出来的键名一致,从而能用一个方法赋值所有成员属性,所见代码量

1Car.m2- (instancetype) initWithDictionary:(NSDictionary *) dictionary {3if(self =[super init]) {4//当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC5[self setValuesForKeysWithDictionary:dictionary];67//self.icon = dictionary[@"icon"];8//self.name = dictionary[@"name"];9}1011returnself;12}13

3.展示标题 setTitle

1/** 设置section头部标题*/2- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

4.索引

1/** 索引*/2- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {3//使用KVC取出数组,不用使用遍历封装4return[self.carGroups valueForKey:@"title"];5}

C.主要代码

1//2//Car.h3//CarGroup4//5//Created by hellovoidworld on 14/12/2.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import<Foundation/Foundation.h>1011@interfaceCar : NSObject1213@property(nonatomic, copy) NSString *icon;14@property(nonatomic, copy) NSString *name;1516- (instancetype) initWithDictionary:(NSDictionary *) dictionary;17+ (instancetype) carWithDictionary:(NSDictionary *) dictionary;18+(instancetype) car;1920@end

1//2//Car.m3//CarGroup4//5//Created by hellovoidworld on 14/12/2.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"Car.h"1011@implementationCar1213- (instancetype) initWithDictionary:(NSDictionary *) dictionary {14if(self =[super init]) {15//当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC16[self setValuesForKeysWithDictionary:dictionary];1718//self.icon = dictionary[@"icon"];19//self.name = dictionary[@"name"];20}2122returnself;23}2425+ (instancetype) carWithDictionary:(NSDictionary *) dictionary {26return[[self alloc] initWithDictionary:dictionary];27}2829+(instancetype) car {30return[self carWithDictionary:nil];31}3233@end

1//2//CarGroup.h3//CarGroup4//5//Created by hellovoidworld on 14/12/2.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import<Foundation/Foundation.h>1011@interfaceCarGroup : NSObject1213@property(nonatomic, strong) NSArray *cars;14@property(nonatomic, copy) NSString *title;1516- (instancetype) initWithDictionary:(NSDictionary *) dictionary;17+ (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary;18+(instancetype) carGroup;1920@end

1//2//CarGroup.m3//CarGroup4//5//Created by hellovoidworld on 14/12/2.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"CarGroup.h"10#import"Car.h"1112@implementationCarGroup1314- (instancetype) initWithDictionary:(NSDictionary *) dictionary {15if(self =[super init]) {16self.title = dictionary[@"title"];1718//这里不能用KVC,封装car model再放到array中,不能直接存储array19NSArray *carsArray = dictionary[@"cars"];20NSMutableArray *mcarsArray =[NSMutableArray array];21for(NSDictionary *dictincarsArray) {22Car *car =[Car carWithDictionary:dict];23[mcarsArray addObject:car];24}2526self.cars =mcarsArray;27}2829returnself;30}3132+ (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary {33return[[self alloc] initWithDictionary:dictionary];34}3536+(instancetype) carGroup {37return[self carGroupWithDictionary:nil];38}3940@end

1//2//ViewController.m3//CarGroup4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"ViewController.h"10#import"CarGroup.h"11#import"Car.h"1213@interfaceViewController () <UITableViewDataSource>14@property (weak, nonatomic) IBOutlet UITableView *tableView;1516//所有的车品牌17@property(nonatomic, strong) NSArray *carGroups;1819@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航