iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

简介: iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一、plist文件和项目结构图 说明:这是一个嵌套模型的示例 二、代码示例:  YYcarsgroup.h文件代码: 1 // 2 // YYcarsgroup.

iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

一、plist文件和项目结构图

说明:这是一个嵌套模型的示例

二、代码示例:

 YYcarsgroup.h文件代码:

 1 //
 2 //  YYcarsgroup.h
 3 //  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 @end

YYcarsgroup.m文件代码:

 1 //
 2 //  YYcarsgroup.m
 3 //  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         //嵌套的字典转模型
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          // 赋值存储模型的数组给属性
28         self.cars=arrayM;
29     }
30     return self;
31 }
32 
33 +(instancetype)carsgroupWithDict:(NSDictionary *)dict
34 {
35     return [[self alloc]initWithDict:dict];
36 }
37 @end

YYcars.h文件

 1 //
 2 //  YYcars.h
 3 //  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.m
 3 //  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 @end

YYViewController.m文件

  1 //
  2 //  YYViewController.m
  3 //  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 @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- 实现懒加载
 29 //1.从包中读取数据
 30 //2.字典转模型
 31 //3.返回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的数据展示
 51 //1.设置数据源,遵守协议
 52 //2.返回组
 53 //3.返回行
 54 //4.每组每行对应的数据
 55 //4.1去缓存中去取cell
 56 //4.2若没有,则创建cell,并盖章
 57 //4.3设置cell的数据
 58 //4.4返回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去缓存中去取cell
 73     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
 74     //4.2若没有,则创建cell,并盖章
 75     if (cell==nil) {
 76         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
 77     }
 78     //4.3设置cell的数据
 79     //设置对应的组
 80     YYcarsgroup *carsgroup=self.car[indexPath.section];
 81     //设置对应的行
 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返回cell
 87     return cell;
 88 }
 89 
 90 //设置每组的标题
 91 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 92 {
 93     YYcarsgroup *carsgroup=self.car[section];
 94     return carsgroup.title;
 95 }
 96 
 97 //设置索引
 98 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
 99 {
100     //利用kvc取出所有的标题
101     NSArray *title=[self.car  valueForKeyPath:@"title"];
102     return title;
103 }
104 
105 //隐藏状态栏
106 -(BOOL)prefersStatusBarHidden
107 {
108     return  YES;
109 }
110 @end

实现效果:

三、注意点

1.设置索引

代码如下:

//设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //利用kvc取出所有的标题
    NSArray *title=[self.car  valueForKeyPath:@"title"];
    return title;
}

2.cell的性能优化

代码如下:

   static NSString *identifier=@"car";
    //4.1去缓存中去取cell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //4.2若没有,则创建cell,并盖章
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

请注意:cell内部数据处理的细节。(如何节省内存?)

目录
相关文章
|
14天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
15 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
25天前
|
存储 运维 安全
iOS加固原理与常见措施:保护移动应用程序安全的利器
iOS加固原理与常见措施:保护移动应用程序安全的利器
28 0
|
2月前
|
Web App开发 JSON JavaScript
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
24 0
|
3月前
|
存储 运维 安全
iOS加固原理与常见措施:保护移动应用程序安全的利器
iOS加固原理与常见措施:保护移动应用程序安全的利器
38 0
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
41 1
|
3月前
|
监控 测试技术 iOS开发
查看ios 应用程序性能
查看ios 应用程序性能
37 0
|
10天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
23天前
|
iOS开发 开发者
iOS移动应用程序的备案与SHA-1值查看
iOS移动应用程序的备案与SHA-1值查看
32 2
|
2月前
|
iOS开发 开发者 索引
批量上传 iOS 应用程序截图的实用技巧
批量上传 iOS 应用程序截图的实用技巧
QGS
|
3月前
|
前端开发 数据可视化 Java
手拉手JavaFX UI控件与springboot3+FX桌面开发(下)
手拉手JavaFX UI控件与springboot3+FX桌面开发
QGS
57 0