iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
生活随笔
收集整理的這篇文章主要介紹了
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
iOS開發(fā)UI篇—使用嵌套模型完成的一個簡單汽車圖標展示程序
一、plist文件和項目結(jié)構(gòu)圖
說明:這是一個嵌套模型的示例
二、代碼示例:
?YYcarsgroup.h文件代碼:
1 //2 // YYcarsgroup.h3 // 07-汽車展示(高級)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 : NSObject 12 @property(nonatomic,copy)NSString *title; 13 @property(nonatomic,strong)NSArray *cars; 14 15 -(instancetype)initWithDict:(NSDictionary *)dict; 16 +(instancetype)carsgroupWithDict:(NSDictionary *)dict; 17 @endYYcarsgroup.m文件代碼:
1 //2 // YYcarsgroup.m3 // 07-汽車展示(高級)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 YYcarsgroup 13 -(instancetype)initWithDict:(NSDictionary *)dict 14 { 15 if (self=[super init]) { 16 //嵌套的字典轉(zhuǎn)模型 17 self.title=dict[@"title"]; 18 19 //注意 20 NSArray *dictcars=dict[@"cars"]; 21 //像下面這樣寫可以提高性能 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 // 賦值存儲模型的數(shù)組給屬性 28 self.cars=arrayM; 29 } 30 return self; 31 } 32 33 +(instancetype)carsgroupWithDict:(NSDictionary *)dict 34 { 35 return [[self alloc]initWithDict:dict]; 36 } 37 @endYYcars.h文件
1 //2 // YYcars.h3 // 07-汽車展示(高級)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 : NSObject 12 @property(nonatomic,copy)NSString *name; 13 @property(nonatomic,copy)NSString *icon; 14 15 -(instancetype)initWithDict:(NSDictionary *)dict; 16 +(instancetype)carsWithDict:(NSDictionary *)dict; 17 @end?YYcars.m文件
1 //2 // YYcars.m3 // 07-汽車展示(高級)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 YYcars 12 13 -(instancetype)initWithDict:(NSDictionary *)dict 14 { 15 if (self=[super init]) { 16 self.name=dict[@"name"]; 17 self.icon=dict[@"icon"]; 18 } 19 return self; 20 } 21 +(instancetype)carsWithDict:(NSDictionary *)dict 22 { 23 return [[self alloc]initWithDict:dict]; 24 } 25 @endYYViewController.m文件
1 //2 // YYViewController.m3 // 07-汽車展示(高級)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 @end17 18 @implementation YYViewController19 20 - (void)viewDidLoad21 {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- 實現(xiàn)懶加載29 //1.從包中讀取數(shù)據(jù)30 //2.字典轉(zhuǎn)模型31 //3.返回cars32 -(NSArray *)car33 {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- 實現(xiàn)tableview的數(shù)據(jù)展示51 //1.設(shè)置數(shù)據(jù)源,遵守協(xié)議52 //2.返回組53 //3.返回行54 //4.每組每行對應(yīng)的數(shù)據(jù)55 //4.1去緩存中去取cell56 //4.2若沒有,則創(chuàng)建cell,并蓋章57 //4.3設(shè)置cell的數(shù)據(jù)58 //4.4返回cell59 60 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView61 {62 return self.car.count;63 }64 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section65 {66 YYcarsgroup *carsgroup=self.car[section];67 return carsgroup.cars.count;68 }69 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath70 {71 static NSString *identifier=@"car";72 //4.1去緩存中去取cell73 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];74 //4.2若沒有,則創(chuàng)建cell,并蓋章75 if (cell==nil) {76 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];77 }78 //4.3設(shè)置cell的數(shù)據(jù)79 //設(shè)置對應(yīng)的組80 YYcarsgroup *carsgroup=self.car[indexPath.section];81 //設(shè)置對應(yīng)的行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返回cell87 return cell;88 }89 90 //設(shè)置每組的標題91 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section92 {93 YYcarsgroup *carsgroup=self.car[section];94 return carsgroup.title;95 }96 97 //設(shè)置索引98 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView99 { 100 //利用kvc取出所有的標題 101 NSArray *title=[self.car valueForKeyPath:@"title"]; 102 return title; 103 } 104 105 //隱藏狀態(tài)欄 106 -(BOOL)prefersStatusBarHidden 107 { 108 return YES; 109 } 110 @end實現(xiàn)效果:
三、注意點
1.設(shè)置索引
代碼如下:
//設(shè)置索引 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {//利用kvc取出所有的標題NSArray *title=[self.car valueForKeyPath:@"title"];return title; }2.cell的性能優(yōu)化
代碼如下:
static NSString *identifier=@"car";//4.1去緩存中去取cellUITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];//4.2若沒有,則創(chuàng)建cell,并蓋章if (cell==nil) {cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}請注意:cell內(nèi)部數(shù)據(jù)處理的細節(jié)。(如何節(jié)省內(nèi)存?)
?
轉(zhuǎn)載于:https://www.cnblogs.com/sunflower-lhb/p/4900840.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 即时聊天IM之一 XMPP协议简述
- 下一篇: How to change the te