• [iOS基础控件


    A.需求
    1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model
    2.使用KVC进行Model封装赋值
    3.展示头字母标题
    4.展示索引(使用KVC代替遍历取出所有索引值)
     
    Image(77)
     
    B.实现
    1.Model嵌套
    其实就是将另一个Model作为成员
     
    .plist 文件结构
    Image(78)
     
    GroupCar中有存储Car的数组
    1 @property(nonatomic, strong) NSArray *cars;
     
    封装Model的时候需要进行相应处理
    CarGroup.m
    1         // 这里不能用KVC,封装car model再放到array中,不能直接存储array
    2         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取出来的键名一致,从而能用一个方法赋值所有成员属性,所见代码量
     1 Car.m
     2 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
     3     if (self = [super init]) {
     4         // 当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC
     5         [self setValuesForKeysWithDictionary:dictionary];
     6        
     7 //        self.icon = dictionary[@"icon"];
     8 //        self.name = dictionary[@"name"];
     9     }
    10    
    11     return self;
    12 }
    13  
    3.展示标题 setTitle
    1 /** 设置section头部标题 */
    2 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
     
    4.索引
    1 /** 索引 */
    2 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    3     // 使用KVC取出数组,不用使用遍历封装
    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 : NSObject
    12 
    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 Car
    12 
    13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
    14     if (self = [super init]) {
    15         // 当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC
    16         [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 : NSObject
    12 
    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 CarGroup
    13 
    14 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
    15     if (self = [super init]) {
    16         self.title = dictionary[@"title"];
    17        
    18         // 这里不能用KVC,封装car model再放到array中,不能直接存储array
    19         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
     
  • 相关阅读:
    并发编程的核心问题
    线程池的作用
    结构化并发编程:并发编程的分解方式与组织形式
    Dispatch Group
    yii视频地址哦
    Redis--各个数据类型最大存储量
    php开发中处理emoji表情和颜文字的兼容问题
    面向对象的三个基本特征(讲解)
    详解Ajax请求(四)——多个异步请求的执行顺序
    red入门学习笔记
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4137101.html
Copyright © 2020-2023  润新知