IOS development UI-a simple car icon display program using nested models

Source: Internet
Author: User

YYcarsgroup. h file code: Copy code 1 // 2 // YYcarsgroup. h 3 // 07-car display (advanced) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface YYcarsgroup: NSObject12 @ property (nonatomic, copy) NSString * title; 13 @ property (nonatomic, strong) NSArray * cars; 14 15-(instancetype) initWithDict :( NSDictionary *) dict; 16 + (instancetype) carsgroupWithDict :( NSDictionary *) dict; 17 @ end copy code YYcarsgroup. m file code: Copy code 1 // 2 // YYcarsgroup. m 3 // 07-car display (advanced) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYcarsgroup. h "10 # import" YYcars. h "11 12 @ implementation YYcarsgroup13-(instancetype) initWithDict :( NSDictionary *) dict14 {15 if (self = [super init]) {16 // nested dictionary to model 17 self. title = dict [@ "title"]; 18 19 // note 20 NSArray * dictcars = dict [@ "cars"]; 21 // write as follows to improve performance 22 NSMutableArray * arrayM = [NSMutableArray arrayWithCapacity: dictcars. count]; 23 for (NSDictionary * dict in dictcars) {24 YYcars * yycars = [[YYcars alloc] initWithDict: dict]; 25 [arrayM addObject: yycars]; 26} 27 // assign a value to the storage model array to attribute 28 self. cars = arrayM; 29} 30 return self; 31} 32 33 + (instancetype) carsgroupWithDict :( NSDictionary *) dict34 {35 return [[self alloc] initWithDict: dict]; 36} 37 @ end copy the code YYcars. h file copy code 1 // 2 // YYcars. h 3 // 07-car display (advanced) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface YYcars: NSObject12 @ property (nonatomic, copy) NSString * name; 13 @ property (nonatomic, copy) NSString * icon; 14 15-(instancetype) initWithDict :( NSDictionary *) dict; 16 + (instancetype) carsWithDict :( NSDictionary *) dict; 17 @ end copy code YYcars. m file copy code 1 // 2 // YYcars. m 3 // 07-car display (advanced) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYcars. h "10 11 @ implementation YYcars12 13-(instancetype) initWithDict :( NSDictionary *) dict14 {15 if (self = [super init]) {16 self. name = dict [@ "name"]; 17 self. icon = dict [@ "icon"]; 18} 19 return self; 20} 21 + (instancetype) carsWithDict :( NSDictionary *) dict22 {23 return [[self alloc] initWithDict: dict]; 24} 25 @ end copy the code YYViewController. m file copy code 1 // 2 // YYViewController. m 3 // 07-car display (advanced) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 # import" YYcarsgroup. h "11 # import" YYcars. h "12 13 @ interface YYViewController () <UITableViewDataSource> 14 @ property (strong, nonatomic) IBOutlet UITableView * tableview; 15 @ property (nonatomic, strong) NSArray * car; 16 @ end 17 18 @ implementation YYViewController 19 20-(void) viewDidLoad 21 {22 [super viewDidLoad]; 23 24 self. tableview. rowHeight = 60.f; 25 self. tableview. dataSource = self; 26 NSLog (@ "% d", self. car. count); 27} 28 # pragma mark-implement lazy loading 29 // 1. read data from the package 30 // 2. dictionary to model 31 // 3. returns cars 32-(NSArray *) car 33 {34 if (_ car = nil) {35 36 NSString * fullpath = [[NSBundle mainBundle] pathForResource: @ "cars_total.plist" ofType: nil]; 37 NSArray * arrayM = [NSArray arrayWithContentsOfFile: fullpath]; 38 39 NSMutableArray * carsarray = [NSMutableArray array]; 40 for (NSDictionary * dict in arrayM) {41 YYcarsgroup * carsgroup = [YYcarsgroup carsgroupWithDict: dict]; 42 [carsarray addObject: carsgroup]; 43} 44 _ car = [carsarray copy]; 45} 46 return _ car; 47} 48 49 50 # pragma mark-tableview data display 51 // 1. set the data source to comply with Protocol 52 // 2. return group 53 // 3. return row 54 // 4. the data corresponding to each row in each group is 55 // 4.1 to fetch the cell 56 // 4.2 in the cache. If no, the cell is created, stamp 57 // 4.3 set the cell data. 58 // 4.4 return cell 59 60-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView 61 {62 return self. car. count; 63} 64-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section 65 {66 YYcarsgroup * carsgroup = self. car [section]; 67 return carsgroup. cars. count; 68} 69-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath 70 {71 static NSString * identifier = @ "car "; 72 // 4.1 fetch cell 73 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identifier] in the cache; 74 // 4.2 if no cell exists, create a cell, stamp 75 if (cell = nil) {76 cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier]; 77} 78 // 4.3 set the cell data 79 // set the corresponding group 80 YYcarsgroup * carsgroup = self. car [indexPath. section]; 81 // set the corresponding row 82 YYcars * yycars = carsgroup. cars [indexPath. row]; 83 84 cell. imageView. image = [UIImage imageNamed: yycars. icon]; 85 cell. textLabel. text = yycars. name; 86 // 4.4 return cell 87 return cell; 88} 89 90 // set the title of each group 91-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section 92 {93 YYcarsgroup * carsgroup = self. car [section]; 94 return carsgroup. title; 95} 96 97 // set the Index 98-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView 99 {100 // use kvc to retrieve all titles. 101 NSArray * title = [self. car valueForKeyPath: @ "title"]; 102 return title; 103} 104 105 // hide Status Bar 106-(BOOL) prefersStatusBarHidden107 {108 return YES; 109} 110 @ 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.