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

简介: iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一、实现效果 说明:该示例在storyboard中使用动态单元格来完成。 二、实现 1.项目文件结构和plist文件 2.实现过程以及代码 在tableview的属性选择器中选择动态单元格。

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;
}

 

目录
相关文章
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
1203 4
|
9月前
|
iOS开发 开发者 Windows
uniapp云打包ios应用证书的获取方法,生成指南
打包用到的一共两个文件,一个是p12格式的私钥证书,一个是证书profile文件。其中生成p12证书的时候,按照官网的教程,是需要MAC电脑来协助做的,主要是生成一些csr文件和导出p12证书等。其实这些步骤也可以借助一些其他的工具来实现,不一定使用mac电脑,用windows电脑也可以创建。
1136 0
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
307 2
|
JSON 前端开发 API
探索iOS开发之旅:打造你的第一个天气应用
【10月更文挑战第36天】在这篇文章中,我们将踏上一段激动人心的旅程,一起构建属于我们自己的iOS天气应用。通过这个实战项目,你将学习到如何从零开始搭建一个iOS应用,掌握基本的用户界面设计、网络请求处理以及数据解析等核心技能。无论你是编程新手还是希望扩展你的iOS开发技能,这个项目都将为你提供宝贵的实践经验。准备好了吗?让我们开始吧!
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
255 1
|
2月前
|
缓存 移动开发 JavaScript
如何优化UniApp开发的App的启动速度?
如何优化UniApp开发的App的启动速度?
445 139
|
2月前
|
移动开发 JavaScript weex
UniApp开发的App在启动速度方面有哪些优势和劣势?
UniApp开发的App在启动速度方面有哪些优势和劣势?
312 137
|
2月前
|
数据采集 JavaScript 前端开发
开发比分App?你缺的不是程序员
开发体育比分App,关键不在代码,而在懂体育、懂数据、懂用户。明确定位、理清需求、选好数据源,再找专业的产品、数据与技术人才协同,才能少走弯路。程序员最后入场,效率最高。
222 154
|
3月前
|
移动开发 小程序 Android开发
基于 uni-app 开发的废品回收类多端应用功能与界面说明
本文将对一款基于 uni-app 开发的废品回收类多端应用,从多端支持范围、核心功能模块及部分界面展示进行客观说明,相关资源信息也将一并呈现。
141 0

热门文章

最新文章