A. Requirements 1. Use the car brand name of the first letter for a model, the car brand for a model, the first letter model nested brand MODEL2. Use KVC for the model package Assignment 3. Display Head Letter Title 4. Display index (use KVC instead of traversal to fetch all index values) B. Implementing 1.Model nesting is actually a member of another model. plist file Structure Groupcar has an array of car storage
1 @property (nonatomic, strong) Nsarray *cars;
The package model needs to be handled accordingly cargroup.m
1 //This can not be used KVC, encapsulated car model and placed in the array, can not directly store array2 nsarray *carsarray = dictionary[@ "Cars"];3 Nsmutablearray *mcarsarray = [Nsmutablearray array];4 for (nsdictionary *dict in Carsarray) {5 car *car = [car car Withdictionary:dict];6 [Mcarsarray addobject:car];7 }8 9 self.cars = Mcarsarray;
2.KVC member names are used in accordance with the key names taken from the dictionary, so that all member properties can be assigned with a single method, the amount of code seen
1 car.m 2-(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary {3 if (self = [super init]) {4 //when DIC You can use KVC 5 [self setvaluesforkeyswithdictionary:dictionary] when the key name in tionary matches the member name in model ; 6 7// Self.icon = dictionary[@ "icon"]; 8// Self.name = dictionary[@ "Name"]; 9 }10 return self;12}13
3. Display Title Settitle
1/** Set section header header */2-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
4. Index
1/** Index */2-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView {3 //Use KVC to remove an array without using traversal encapsulation 4 return [self.cargroups valueforkey:@ "title"];5}
C. Main code
1//2// Car.h 3// Cargroup 4//5// Created by Hellovoidworld on 14/12/2.6// Copyright (c) 2014 Hel Lovoidworld. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @interface car:nsobject12 @property (nonatomic, copy) Nsstri ng *icon;14 @property (nonatomic, copy) NSString *name;15-(Instancetype) Initwithdictionary: (Nsdictionary *) Dictiona Ry;17 + (Instancetype) carwithdictionary: (Nsdictionary *) dictionary;18 + (instancetype) car;19 @end
1//2// CAR.M 3// Cargroup 4//5// Created by Hellovoidworld on 14/12/2.6// Copyright (c) 2014 Hel Lovoidworld. All rights reserved. 7//8 9 #import "Car.h" @implementation Car12-(Instancetype) Initwithdictionary: (Nsdictionary *) dictionary { if (self = [super init]) { //when the key name in dictionary is the same as the member name in model, you can use KVC16 [self Setvaluesforkeyswithdictionary:dictionary];17 // Self.icon = dictionary[@ "icon"];19// Self.name = dictionary[@ "Name"];20 }21 return self;23}24 + (instancetype) carwithdictionary: (Nsdictionary * ) dictionary {[self alloc] initwithdictionary:dictionary];27}28 + (instancetype) car { return [ Self carwithdictionary:nil];31}32 @end
1//2// CarGroup.h 3// Cargroup 4//5// Created by Hellovoidworld on 14/12/2.6// Copyright (c) 2014 Year Hellovoidworld. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @interface cargroup:nsobject12 @property (nonatomic, Strong) Nsarray *cars;14 @property (nonatomic, copy) NSString *title;15-(Instancetype) Initwithdictionary: (Nsdictionary *) di Ctionary;17 + (Instancetype) cargroupwithdictionary: (Nsdictionary *) dictionary;18 + (instancetype) carGroup;19 @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" #import "Car.h" @implementation CarGroup13-(Instancetype) Initwithdictionary: (N Sdictionary *) Dictionary {if (self = [super init]) {self.title = dictionary[@ "title"];17 18 The KVC can not be used here to encapsulate the car model and put it into an array, not directly stored array19 nsarray *carsarray = dictionary[@ "Cars"];20 nsmutablear Ray *mcarsarray = [Nsmutablearray array];21 for (nsdictionary *dict in Carsarray) {car *car = [car] carwithdictionary:dict];23 [Mcarsarray addobject:car];24}25 self.cars = Mcarsarray; }28 return self;30}31 + (instancetype) cargroupwithdictionary: (Nsdictionary *) dictionary {£ º RET urn [[Self alloc] initwithdictionary:dictionary];34}35 + (instancetype) cargroup {Notoginseng return [self cargroupwithdictionary:nil];38}39 @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" #import "CarGroup.h" #import "Car.h" @interface Viewcontroller () <uit Ableviewdatasource>14 @property (Weak, nonatomic) Iboutlet UITableView *tableview;15 16//All Car brands @property ( Nonatomic, Strong) Nsarray *cargroups;18 @end
iOS Base controls Auto brand showcase model nested/kvc/tableview index