国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[iOS基礎(chǔ)控件-6.4]汽車品牌展示Model嵌套/KVC/TableView索引

2019-11-14 19:48:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
A.需求
1.使用汽車品牌名稱頭字母為一個(gè)Model,汽車品牌為一個(gè)Model,頭字母Model嵌套品牌Model
2.使用KVC進(jìn)行Model封裝賦值
3.展示頭字母標(biāo)題
4.展示索引(使用KVC代替遍歷取出所有索引值)
 
Image(77)
 
B.實(shí)現(xiàn)
1.Model嵌套
其實(shí)就是將另一個(gè)Model作為成員
 
.plist 文件結(jié)構(gòu)
Image(78)
 
GroupCar中有存儲(chǔ)Car的數(shù)組
1 @PRoperty(nonatomic, strong) NSArray *cars;
 
封裝Model的時(shí)候需要進(jìn)行相應(yīng)處理
CarGroup.m
1         // 這里不能用KVC,封裝car model再放到array中,不能直接存儲(chǔ)array2         NSArray *carsArray = dictionary[@"cars"];3         NSMutableArray *mcarsArray = [NSMutableArray array];4         for (NSDictionary *dict in carsArray) {5             Car *car = [Car carWithDictionary:dict];6             [mcarsArray addObject:car];7         }8       9         self.cars = mcarsArray;
 
2.KVC
成員名使用和從dictionary取出來(lái)的鍵名一致,從而能用一個(gè)方法賦值所有成員屬性,所見(jiàn)代碼量
 1 Car.m 2 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 3     if (self = [super init]) { 4         // 當(dāng)dictionary中的鍵名跟Model中的成員名一致的時(shí)候,可以使用KVC 5         [self setValuesForKeysWithDictionary:dictionary]; 6         7 //        self.icon = dictionary[@"icon"]; 8 //        self.name = dictionary[@"name"]; 9     }10    11     return self;12 }13  

 

3.展示標(biāo)題 setTitle
1 /** 設(shè)置section頭部標(biāo)題 */2 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 
4.索引
1 /** 索引 */2 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {3     // 使用KVC取出數(shù)組,不用使用遍歷封裝4     return [self.carGroups valueForKey:@"title"];5 }
 
C.主要代碼
 1 // 2 //  Car.h 3 //  CarGroup 4 // 5 //  Created by hellovoidworld on 14/12/2. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface Car : NSObject12 13 @property(nonatomic, copy) NSString *icon;14 @property(nonatomic, copy) NSString *name;15 16 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;17 + (instancetype) carWithDictionary:(NSDictionary *) dictionary;18 + (instancetype) car;19 20 @end
 1 // 2 //  Car.m 3 //  CarGroup 4 // 5 //  Created by hellovoidworld on 14/12/2. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "Car.h"10 11 @implementation Car12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {14     if (self = [super init]) {15         // 當(dāng)dictionary中的鍵名跟Model中的成員名一致的時(shí)候,可以使用KVC16         [self setValuesForKeysWithDictionary:dictionary];17        18 //        self.icon = dictionary[@"icon"];19 //        self.name = dictionary[@"name"];20     }21    22     return self;23 }24 25 + (instancetype) carWithDictionary:(NSDictionary *) dictionary {26     return [[self alloc] initWithDictionary:dictionary];27 }28 29 + (instancetype) car {30     return [self carWithDictionary:nil];31 }32 33 @end
 
 1 // 2 //  CarGroup.h 3 //  CarGroup 4 // 5 //  Created by hellovoidworld on 14/12/2. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface CarGroup : NSObject12 13 @property(nonatomic, strong) NSArray *cars;14 @property(nonatomic, copy) NSString *title;15 16 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;17 + (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary;18 + (instancetype) carGroup;19 20 @end
 
 1 // 2 //  CarGroup.m 3 //  CarGroup 4 // 5 //  Created by hellovoidworld on 14/12/2. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "CarGroup.h"10 #import "Car.h"11 12 @implementation CarGroup13 14 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {15     if (self = [super init]) {16         self.title = dictionary[@"title"];17        18         // 這里不能用KVC,封裝car model再放到array中,不能直接存儲(chǔ)array19         NSArray *carsArray = dictionary[@"cars"];20         NSMutableArray *mcarsArray = [NSMutableArray array];21         for (NSDictionary *dict in carsArray) {22             Car *car = [Car carWithDictionary:dict];23             [mcarsArray addObject:car];24         }25        26         self.cars = mcarsArray;27     }28    29     return self;30 }31 32 + (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary {33     return [[self alloc] initWithDictionary:dictionary];34 }35 36 + (instancetype) carGroup {37     return [self carGroupWithDictionary:nil];38 }39 40 @end
 
 1 // 2 //  ViewController.m 3 //  CarGroup 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "CarGroup.h"11 #import "Car.h"12 13 @interface ViewController () <UITableViewDataSource>14 @property (weak, nonatomic) IBOutlet UITableView *tableView;15 16 // 所有的車品牌17 @property(nonatomic, strong) NSArray *carGroups;18 19 @end

 

 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝丰县| 英超| 休宁县| 黄大仙区| 霞浦县| 富蕴县| 平果县| 长沙市| 湘乡市| 三门县| 渝中区| 镇原县| 方山县| 德化县| 威海市| 许昌市| 云梦县| 龙川县| 定陶县| 吴桥县| 永州市| 荔浦县| 德化县| 武功县| 高要市| 赤城县| 凭祥市| 牡丹江市| 香河县| 沭阳县| 黄浦区| 隆尧县| 山阳县| 寿宁县| 鹿泉市| 垦利县| 晋江市| 宁德市| 黄浦区| 甘泉县| 晋江市|