iOS Development UI Chapter-a simple car icon display program using nested models

Source: Internet
Author: User

iOS Development UI Chapter-a simple car icon display program using nested models

I.. plist document and Project structure chart

Description: This is an example of a nested model

Second, the code example:

YYcarsgroup.h File Code:

1//2//  YYcarsgroup.h 3//  07-Auto Show (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 @interface yycarsgroup:nsobject12 @property (nonatomic,copy) NSString *title;13 @property (nonatomic,strong) Nsarray *cars;14 (instancetype) initwithdict: (Nsdictionary *) dict; + (Instancetype) carsgroupwithdict: (nsdictionary *) dict;17 @end

YYCARSGROUP.M File Code:

1//2//  yycarsgroup.m 3//  07-Auto Show (Advanced) 4//5//  Created by Apple on 14-5-28.6//  Copyright (c) 2014 I Tcase. All rights reserved. 7//8  9 #import "YYcarsgroup.h" #import "YYcars.h" @implementation YYcarsgroup13-(instancetype) initwithdict :(Nsdictionary *) dict14 {     if (self=[super init]) {         //nested dictionary to model         self.title=dict[@ "title"];18         19         //Note         Nsarray *dictcars=dict[@ "Cars"];21         //Like the following can be said to mention high performance,         Nsmutablearray *arraym=[nsmutablearray Arraywithcapacity:dictcars.count];23 for         (nsdictionary *dict in  dictcars) {             yycars *yycars=[[ Yycars alloc]initwithdict:dict];25             [arraym addobject:yycars];26         }27          //An array of assignment storage models to properties         self.cars=arraym;29     }30     return self;31}32 + (instancetype) carsgroupwithdict: (Nsdictionary *) Dict34 {35     return [[Self alloc]initwithdict:dict];36}37 @end

YYcars.h file

1//2//  YYcars.h 3//  07-Auto Show (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 @interface yycars:nsobject12 @property (nonatomic,copy) NSString *name;13 @property (nonatomic,copy) nsstring *icon;14-(Instancetype) Initwithdict: (Nsdictionary *) dict;16 + (instancetype) Carswithdict: (nsdictionary *) dict;17 @end

YYCARS.M file

1//2//  YYCARS.M 3//  07-Auto Show (Advanced) 4//5//  Created by Apple on 14-5-28.6//  Copyright (c) 2014 itcase . All rights reserved. 7//8  9 #import "YYcars.h" @implementation YYcars12-(Instancetype) Initwithdict: (Nsdictionary *) dict14 {15
   if (Self=[super init]) {         self.name=dict[@ "name"];17         self.icon=dict[@ "icon"];18     }19     return Self;20}21 + (instancetype) carswithdict: (Nsdictionary *) dict22 {     return [[Self alloc]initwithdict:dict];24}25 @end

YYVIEWCONTROLLER.M file

  1//2//YYVIEWCONTROLLER.M 3//07-Auto Show (Advanced) 4//5//Created by Apple on 14-5-28. 6//Copyright (c) 2014 itcase.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYcarsgroup.h" #import "YYcars.h" @interface Yyviewcontroll ER () <UITableViewDataSource> @property (Strong, nonatomic) Iboutlet UITableView *tableview; @property (Nonatomic,strong) Nsarray *car;     @end @implementation Yyviewcontroller-(void) viewdidload {[Super viewdidload]; 23 24 SELF.TABLEVIEW.ROWHEIGHT=60.F; self.tableview.datasource=self; NSLog (@ "%d", self.car.count);        #pragma mark-to implement lazy loading 29//1. Read data from a package 30//2. Dictionary to Model 31//3. Return Cars-(Nsarray *) car (*) NSString *fullpath= [[NSBundle mainbundle]pathforresource:@ "Cars_total.plist" oftype:nil]; Panax Notoginseng nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath]; Nsmutablearray *carsarray=[Nsmutablearray array]; (Nsdictionary *dict in Arraym) {Yycarsgroup *carsgroup=[yycarsgroup carsgroupwithdict:dict ]; [Carsarray Addobject:carsgroup]; _car=[carsarray copy]; The _car return; The #pragma mark-to achieve tableview data show 51//1. Set up a data source that complies with protocol 52//2. Returns the group 53//3. Returns row 54//4. Data for each group per row 55//4.1 go to fetch cel in cache L 56//4.2 If not, create cell and stamp 57//4.3 set cell data 58//4.4 return cell-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView (Self.car.count)-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section (Yycarsgroup) (*carsgroup=self.car[section); UIT return carsgroup.cars.count; Ableviewcell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {+-static Nsstri Ng *[email protected] "car"; 72//4.1 go to cache to fetch cell UITableViewCell *cell=[tableview dequeuereusablecellwithIdentifier:identifier]; 74//4.2 If not, create a cell and stamp the IF (cell==nil) {Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcells Tyledefault Reuseidentifier:identifier]; 77} 78//4.3 set data for cell 79//Set corresponding group Yycarsgroup *carsgroup=self.car[indexpath.section]; 81//Set corresponding line of Yycars *yycars=carsgroup.cars[indexpath.row]; Cell.imageview.image=[uiimage ImageNamed:yycars.icon]; Cell.textlabel.text=yycars.name; 86//4.4 return cell; 88} 89 90//Set title for each group (NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section 92 {9 3 Yycarsgroup *carsgroup=self.car[section]; 94 return carsgroup.title;     95} 96 97//SET Index 98-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView 99 {100//Use KVC to remove all headers 101 Nsarray *title=[self.car valueforkeypath:@ "title"];102 return title;103}104 105//Hide Status bar 106-(BOOL) Prefersstatusbar Hidden107 {108 return yes;109}110 @end

Implementation results:

Third, the attention point

1. Setting the Index

The code is as follows:

Set index-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{    //Use KVC to remove all headers    Nsarray *title= [Self.car  valueforkeypath:@ "title"];    return title;}

Performance optimization for 2.cell

The code is as follows:

   Static nsstring *[email protected] "car";    4.1 Go to cache to fetch cell    UITableViewCell *cell=[tableview dequeuereusablecellwithidentifier:identifier];    4.2 If not, create the cell and stamp    if (cell==nil) {        Cell=[[uitableviewcell Alloc]initwithstyle: Uitableviewcellstyledefault reuseidentifier:identifier];    }

iOS Development UI Chapter-a simple car icon display program using nested models

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.