[iOS base Control-6.4] auto brand showcase model nested/kvc/tableview index

Source: Internet
Author: User

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         //You cannot use KVC to encapsulate the car model and place it in an array, and you cannot store the array directly .2Nsarray *carsarray = dictionary[@"Cars"];3Nsmutablearray *mcarsarray =[Nsmutablearray array];4          for(Nsdictionary *dictinchCarsarray) {5Car *car =[Car carwithdictionary:dict];6 [Mcarsarray Addobject:car];7         }8       9Self.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.M2-(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary {3     if(self =[Super Init]) {4         //when the key name in the dictionary is consistent with the member name in model, you can use KVC5 [self setvaluesforkeyswithdictionary:dictionary];6        7 //Self.icon = dictionary[@ "icon"];8 //self.name = dictionary[@ "name"];9     }Ten     One     returnSelf ; A } -  

3. Display Title Settitle
1 /*  */2 -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) Section
4. Index
1 /*  */2 -(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView {3      //  use KVC to remove the array without using traverse wrapping 4     return [self.cargroups valueforkey: @" title " ]; 5 }
C. Main code
1 //2 //Car.h3 //Cargroup4 //5 //Created by Hellovoidworld on 14/12/2.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceCar:nsobject A  -@property (nonatomic, copy) NSString *icon; -@property (nonatomic, copy) NSString *name; the  --(Instancetype) Initwithdictionary: (Nsdictionary *) dictionary; -+ (Instancetype) carwithdictionary: (Nsdictionary *) dictionary; -+(instancetype) car; +  - @end
1 //2 //CAR.M3 //Cargroup4 //5 //Created by Hellovoidworld on 14/12/2.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import "Car.h"Ten  One @implementationCar A  --(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary { -     if(self =[Super Init]) { the         //when the key name in the dictionary is consistent with the member name in model, you can use KVC - [self setvaluesforkeyswithdictionary:dictionary]; -         - //Self.icon = dictionary[@ "icon"]; + //self.name = dictionary[@ "name"]; -     } +     A     returnSelf ; at } -  -+ (Instancetype) carwithdictionary: (Nsdictionary *) Dictionary { -     return[[Self alloc] initwithdictionary:dictionary]; - } -  in+(instancetype) car { -     return[self carwithdictionary:nil]; to } +  - @end
1 //2 //CarGroup.h3 //Cargroup4 //5 //Created by Hellovoidworld on 14/12/2.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceCargroup:nsobject A  -@property (nonatomic, strong) Nsarray *cars; -@property (nonatomic, copy) NSString *title; the  --(Instancetype) Initwithdictionary: (Nsdictionary *) dictionary; -+ (Instancetype) cargroupwithdictionary: (Nsdictionary *) dictionary; -+(instancetype) cargroup; +  - @end
1 //2 //cargroup.m3 //Cargroup4 //5 //Created by Hellovoidworld on 14/12/2.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import "CarGroup.h"Ten #import "Car.h" One  A @implementationCargroup -  --(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary { the     if(self =[Super Init]) { -Self.title = dictionary[@"title"]; -         -         //You cannot use KVC to encapsulate the car model and place it in an array, and you cannot store the array directly . +Nsarray *carsarray = dictionary[@"Cars"]; -Nsmutablearray *mcarsarray =[Nsmutablearray array]; +          for(Nsdictionary *dictinchCarsarray) { ACar *car =[Car carwithdictionary:dict]; at [Mcarsarray Addobject:car]; -         } -         -Self.cars =Mcarsarray; -     } -     in     returnSelf ; - } to  ++ (Instancetype) cargroupwithdictionary: (Nsdictionary *) Dictionary { -     return[[Self alloc] initwithdictionary:dictionary]; the } *  $+(instancetype) cargroup {Panax Notoginseng     return[self cargroupwithdictionary:nil]; - } the  + @end
1 //2 //VIEWCONTROLLER.M3 //Cargroup4 //5 //Created by Hellovoidworld on 14/12/1.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import "CarGroup.h" One #import "Car.h" A  - @interfaceViewcontroller () <UITableViewDataSource> -@property (Weak, nonatomic) iboutlet UITableView *TableView; the  - //all the car brands -@property (nonatomic, strong) Nsarray *cargroups; -  + @end

[iOS base Control-6.4] auto brand showcase model nested/kvc/tableview index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.