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

 

目录
相关文章
|
存储 SQL 虚拟化
Omnissa App Volumes 4, version 2503 - 实时应用程序交付系统
Omnissa App Volumes 4, version 2503 - 实时应用程序交付系统
331 7
Omnissa App Volumes 4, version 2503 - 实时应用程序交付系统
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
1028 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
690 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
1946 11
|
人工智能 程序员 API
iOS|记一名 iOS 开发新手的前两次 App 审核经历
啥,这玩意也有新手保护期?
539 0
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
545 66
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
484 3
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。