[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. requirement 1. use the car brand name header letters as a Model, the car brand as a Model, the first letter Model nested brand Model2. use KVC for Model encapsulation assignment 3. show the first letter Title 4. display indexes (use KVC instead of traversing to retrieve all index values) 1 @ property (nonatomic, strong) NSArray * cars;When encapsulating the Model, you need to process CarGroup. m accordingly.

1 // KVC cannot be used here, and the car model cannot be encapsulated in the array. array2 NSArray * carsArray = dictionary [@ "cars"] cannot be stored directly; 3 NSMutableArray * mcarsArray = [NSMutableArray array]; 4 (NSDictionary * dict in carsArray) {5 Car * car = [Car carWithDictionary: dict]; 6 [mcarsArray addObject: car]; 7} 8 9 self. cars = mcarsArray;
2. The KVC member name must be the same as the key name obtained from the dictionary, so that you can assign values to all member attributes in one way. What you see is the amount of code.
1 Car. m 2-(instancetype) initWithDictionary :( NSDictionary *) dictionary {3 if (self = [super init]) {4 // when the key name in the dictionary is the same as the member name in the Model, you can use KVC 5 [self setValuesForKeysWithDictionary: dictionary]; 6 7/self. icon = dictionary [@ "icon"]; 8 // self. name = dictionary [@ "name"]; 9} 10 11 return self; 12} 13

 

3. Display title setTitle
1/** set the section header title */2-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
4. Index
1/** Index */2-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {3 // use KVC to retrieve the array and do not use traversal to encapsulate 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 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 // when the key name in the dictionary is the same as the member name in the Model, you can use 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 cannot be used here, and the car model cannot be encapsulated in the array, cannot directly store 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 // all car brands 17 @ property (nonatomic, strong) NSArray * carGroups; 18 19 @ end

 

 

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.