iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

简介:

一、实现效果

说明:该示例在storyboard中使用动态单元格来完成。

二、实现

1.项目文件结构和plist文件

2.实现过程以及代码

在tableview的属性选择器中选择动态单元格。

说明:在storyboard中直接使用其自带的动态单元格完成tableviewcell的定义,并创建了一个管理该cell的类,进行了连线。

实现代码:

数据模型部分:

YYappInfo.h文件

复制代码
 1 //  2 // YYappInfo.h
 3 // 01-使用动态单元格来完成app应用程序管理界面的搭建
 4 //  5 // Created by 孔医己 on 14-6-2.
 6 // Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8  9 #import <Foundation/Foundation.h>
10 11 @interface YYappInfo : NSObject
12 @property(nonatomic,copy)NSString *size;
13 @property(nonatomic,copy)NSString *download;
14 @property(nonatomic,copy)NSString *icon;
15 @property(nonatomic,copy)NSString *name;
16 17 18 19 -(instancetype)initWithDict:(NSDictionary *)dict;
20 +(instancetype)appInfoWithDict:(NSDictionary *)dict;
21 @end
复制代码

YYappInfo.m文件

复制代码
 1 //  2 // YYappInfo.m
 3 // 01-使用动态单元格来完成app应用程序管理界面的搭建
 4 //  5 // Created by 孔医己 on 14-6-2.
 6 // Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8  9 #import "YYappInfo.h" 10 11 @implementation YYappInfo
12 13 -(instancetype)initWithDict:(NSDictionary *)dict
14 {
15 if (self=[super init]) {
16 //使用KVC 17  [self setValuesForKeysWithDictionary:dict];
18  }
19 return self;
20 }
21 22 23 +(instancetype)appInfoWithDict:(NSDictionary *)dict
24 {
25 26 return [[self alloc]initWithDict:dict];
27 }
28 @end
复制代码

视图部分

YYappCell.h文件

复制代码
 1 //  2 // YYappCell.h
 3 // 01-使用动态单元格来完成app应用程序管理界面的搭建
 4 //  5 // Created by 孔医己 on 14-6-2.
 6 // Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8  9 #import <UIKit/UIKit.h>
10 11 12 @class YYappInfo,YYappCell;
13 14 @protocol YYappCellDelegate <NSObject>
15 -(void)btnDidClick:(YYappCell *)cell;
16 17 18 @end 19 @interface YYappCell : UITableViewCell
20 21 @property(nonatomic,strong)YYappInfo *app;
22 //@property(nonatomic,strong)YYViewController *controller; 23 @property(nonatomic,strong)id <YYappCellDelegate> delegate;
24 25 @end
复制代码

YYappCell.m文件

复制代码
 1 //  2 // YYappCell.m
 3 // 01-使用动态单元格来完成app应用程序管理界面的搭建
 4 //  5 // Created by 孔医己 on 14-6-2.
 6 // Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8  9 #import "YYappCell.h" 10 #import "YYappInfo.h" 11 12 @interface YYappCell ()
13 @property (weak, nonatomic) IBOutlet UIImageView *appimg;
14 15 @property (weak, nonatomic) IBOutlet UILabel *apptitle;
16 @property (weak, nonatomic) IBOutlet UILabel *appdownload;
17 @property (weak, nonatomic) IBOutlet UIButton *appbtn;
18 19 @end 20 @implementation YYappCell
21 22 23 -(void)setApp:(YYappInfo *)app
24 {
25 _app=app;
26 self.apptitle.text=_app.name;
27 self.appdownload.text=[NSString stringWithFormat:@"大小 %@ | 下载量 %@次",_app.size,_app.download];
28 self.appimg.image=[UIImage imageNamed:_app.icon];
29 30 }
31 32 #pragma mark- 完成按钮点击事件
33 34 - (IBAction)btnOnclick:(UIButton *)sender
35 {
36 //按钮被点击后,变为不可用状态 37 sender.enabled=NO;
38 39 //通知代理,完成提示下载已经完成的动画效果 40 if ([self.delegate respondsToSelector:@selector(btnDidClick:)]) {
41 //一般而言,谁触发就把谁传过去 42 [self.delegate btnDidClick:self];
43  }
44 }
45 46 @end
复制代码

主控制器

YYViewController.m文件

复制代码
 1 //  2 // YYViewController.m
 3 // 01-使用动态单元格来完成app应用程序管理界面的搭建
 4 //  5 // Created by 孔医己 on 14-6-2.
 6 // Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8  9 #import "YYViewController.h"  10 #import "YYappInfo.h"  11 #import "YYappCell.h"  12  13 @interface YYViewController ()<UITableViewDataSource,YYappCellDelegate>
 14 @property(nonatomic,strong)NSArray *apps;
 15 @property (strong, nonatomic) IBOutlet UITableView *tableview;
 16  17 @end  18  19 @implementation YYViewController
 20  21 - (void)viewDidLoad
 22 {
 23  [super viewDidLoad];
 24 }
 25  26 #pragma mark- 使用懒加载先把plist文件中得数据加载进来
 27 -(NSArray *)apps
 28 {
 29 if (_apps==Nil) {
 30 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"apps_full.plist" ofType:Nil];
 31 NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
 32  33 NSMutableArray *modles=[NSMutableArray arrayWithCapacity:arrayM.count];
 34 for (NSDictionary *dict in arrayM) {
 35 YYappInfo *appinfo=[YYappInfo appInfoWithDict:dict];
 36  [modles addObject:appinfo];
 37  }
 38 _apps=[modles copy];
 39  }
 40 return _apps;
 41 }
 42  43  44 #pragma mark- 设置tableview的数据源方法
 45 //  46 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 47 {
 48 return 1;
 49 }
 50 //  51 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 52 {
 53 return self.apps.count;
 54 }
 55 //组-行-数据  56 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 57 {
 58 //创建cell  59 static NSString *identifier=@"app";
 60 YYappCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
 61 //设置cell的数据  62 YYappInfo *appinfo=self.apps[indexPath.row];
 63 //设置代理  64 cell.delegate=self;
 65 cell.app=appinfo;
 66 //返回cell  67 return cell;
 68 }
 69  70 #pragma mark- 设置代理
 71 -(void)btnDidClick:(YYappCell *)cell
 72 {
 73 //取出模型  74 YYappInfo *app=cell.app;
 75 NSLog(@"daili");
 76 UILabel *lab=[[UILabel alloc]init];
 77 //提示的显示位置  78 lab.frame=CGRectMake(60, 300, 200, 20);
 79 //设置提示文本  80 lab.text=[NSString stringWithFormat:@"%@已经下载完成",app.name];
 81 //设置文本背景颜色  82  [lab setBackgroundColor:[UIColor grayColor]];
 83  [self.view addSubview:lab];
 84 lab.alpha=1.0;
 85  86 //设置动画效果  87 [UIView animateWithDuration:2.0 animations:^{
 88 lab.alpha=0.0;
 89 } completion:^(BOOL finished) {
 90 //把弹出的提示信息从父视图中删除  91  [lab removeFromSuperview];
 92  }];
 93 }
 94  95 #pragma mark-隐藏状态栏
 96 -(BOOL)prefersStatusBarHidden
 97 {
 98 return YES;
 99 }
100 101 @end
复制代码

补充说明

  在程序中通过标示符取出对应的cell,是因为在storyboard中已经对cell打上了标示符(app)的标签。

复制代码
//组-行-数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 //创建cell static NSString *identifier=@"app"; YYappCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
 //设置cell的数据
 YYappInfo *appinfo=self.apps[indexPath.row];
 //设置代理
 cell.delegate=self;
 cell.app=appinfo;
 //返回cell return cell;
}
复制代码
目录
相关文章
|
5天前
|
C# Android开发 开发者
Uno Platform 高级定制秘籍:深度解析与实践样式和模板应用,助你打造统一且高效的跨平台UI设计
【9月更文挑战第7天】Uno Platform 是一个强大的框架,支持使用 C# 和 XAML 创建跨平台 UI 应用,覆盖 Windows、iOS、Android、macOS 和 WebAssembly。本文介绍 Uno Platform 中样式和模板的应用,助力开发者提升界面一致性与开发效率。样式定义控件外观,如颜色和字体;模板则详细定制控件布局。通过 XAML 定义样式和模板,并可在资源字典中全局应用或嵌套扩展。合理利用样式和模板能简化代码、保持设计一致性和提高维护性,帮助开发者构建美观高效的跨平台应用。
14 1
|
11天前
|
vr&ar C# 图形学
WPF与AR/VR的激情碰撞:解锁Windows Presentation Foundation应用新维度,探索增强现实与虚拟现实技术在现代UI设计中的无限可能与实战应用详解
【8月更文挑战第31天】增强现实(AR)与虚拟现实(VR)技术正迅速改变生活和工作方式,在游戏、教育及工业等领域展现出广泛应用前景。本文探讨如何在Windows Presentation Foundation(WPF)环境中实现AR/VR功能,通过具体示例代码展示整合过程。尽管WPF本身不直接支持AR/VR,但借助第三方库如Unity、Vuforia或OpenVR,可实现沉浸式体验。例如,通过Unity和Vuforia在WPF中创建AR应用,或利用OpenVR在WPF中集成VR功能,从而提升用户体验并拓展应用功能边界。
25 0
|
11天前
|
C# 开发者 设计模式
WPF开发者必读:命令模式应用秘籍,轻松简化UI与业务逻辑交互,让你的代码更上一层楼!
【8月更文挑战第31天】在WPF应用开发中,命令模式是简化UI与业务逻辑交互的关键技术,通过将请求封装为对象,实现UI操作与业务逻辑分离,便于代码维护与扩展。本文介绍命令模式的概念及实现方法,包括使用`ICommand`接口、`RelayCommand`类及自定义命令等方式,并提供示例代码展示如何在项目中应用命令模式。
20 0
|
11天前
|
开发者 Android开发 UED
打造流畅应用:深入探索如何在Xamarin项目中选择并实现最佳UI/UX设计的实践指南
【8月更文挑战第31天】在数字化时代,UI/UX设计成为应用成功的关键。Xamarin以高效开发和强大兼容性著称,其设计理念“一次编写,处处运行”需充分适应多平台特性,提供一致体验。选择Xamarin.Forms或结合Xamarin.Native可实现跨平台UI设计;遵循各平台设计指南,保持布局一致性和简洁性;通过用户测试不断优化。最终,结合技术和用户需求,打造美观实用的应用,脱颖而出。
22 0
|
4月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
4月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
4月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
|
4月前
|
JSON 前端开发 测试技术
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
|
4月前
|
缓存 JavaScript 前端开发
如何理解 SAP UI5 的 sap.ui.define 函数?
如何理解 SAP UI5 的 sap.ui.define 函数?