iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5886字,纯文字阅读大概需要9分钟。
内容图文
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> 1011@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; 16171819 -(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{ 15if (self=[super init]) { 16//使用KVC17 [self setValuesForKeysWithDictionary:dict]; 18 } 19return self; 20} 212223 +(instancetype)appInfoWithDict:(NSDictionary *)dict 24{ 2526return [[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> 101112@class YYappInfo,YYappCell; 1314@protocol YYappCellDelegate <NSObject> 15 -(void)btnDidClick:(YYappCell *)cell; 161718@end19@interface YYappCell : UITableViewCell 2021 @property(nonatomic,strong)YYappInfo *app; 22//@property(nonatomic,strong)YYViewController *controller;23 @property(nonatomic,strong)id <YYappCellDelegate> delegate; 2425@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; 1415 @property (weak, nonatomic) IBOutlet UILabel *apptitle; 16 @property (weak, nonatomic) IBOutlet UILabel *appdownload; 17 @property (weak, nonatomic) IBOutlet UIButton *appbtn; 1819@end20@implementation YYappCell 212223 -(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]; 2930} 3132#pragma mark- 完成按钮点击事件 3334 - (IBAction)btnOnclick:(UIButton *)sender 35{ 36//按钮被点击后,变为不可用状态37 sender.enabled=NO; 3839//通知代理,完成提示下载已经完成的动画效果40if ([self.delegate respondsToSelector:@selector(btnDidClick:)]) { 41//一般而言,谁触发就把谁传过去42 [self.delegate btnDidClick:self]; 43 } 44} 4546@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{ 29if (_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]; 34for (NSDictionary *dict in arrayM) { 35 YYappInfo *appinfo=[YYappInfo appInfoWithDict:dict]; 36 [modles addObject:appinfo]; 37 } 38 _apps=[modles copy]; 39 } 40return _apps; 41} 42 43 44#pragma mark- 设置tableview的数据源方法 45//组 46 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 47{ 48return1; 49} 50//行 51 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 52{ 53return self.apps.count; 54} 55//组-行-数据 56 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 57{ 58//创建cell 59static 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 67return 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{ 98return YES; 99} 100101@end
补充说明
在程序中通过标示符取出对应的cell,是因为在storyboard中已经对cell打上了标示符(app)的标签。
// 组-行-数据 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //创建cellstatic NSString *identifier=@"app";YYappCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; //设置cell的数据 YYappInfo *appinfo=self.apps[indexPath.row]; //设置代理 cell.delegate=self; cell.app=appinfo; //返回cellreturn cell; }
原文:http://www.cnblogs.com/wendingding/p/3764173.html
内容总结
以上是互联网集市为您收集整理的iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建全部内容,希望文章能够帮你解决iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。