一、Model
1 // 2 // BWApp.h 3 // IOS_0112_应用管理 4 // 5 // Created by ma c on 16/1/12. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import10 11 @interface BWApp : NSObject12 13 @property (nonatomic, copy) NSString *size;14 @property (nonatomic, copy) NSString *download;15 @property (nonatomic, copy) NSString *name;16 @property (nonatomic, copy) NSString *icon;17 //标记是否被下载过18 @property (nonatomic, assign) BOOL isDownloaded;19 20 - (instancetype)initWithDict:(NSDictionary *)dict;21 + (instancetype)appWithDict:(NSDictionary *)dict;22 23 @end24 25 26 //27 // BWApp.m28 // IOS_0112_应用管理29 //30 // Created by ma c on 16/1/12.31 // Copyright (c) 2016年 博文科技. All rights reserved.32 //33 34 #import "BWApp.h"35 36 @implementation BWApp37 38 - (instancetype)initWithDict:(NSDictionary *)dict39 {40 if (self = [super init]) {41 [self setValuesForKeysWithDictionary:dict];42 }43 return self;44 }45 46 + (instancetype)appWithDict:(NSDictionary *)dict47 {48 return [[self alloc] initWithDict:dict];49 }50 51 @end
二、View
1 #import2 @class BWAppCell; 3 @protocol appCellDelegate 4 5 - (void)btnDownloadClick:(BWAppCell *)appCell; 6 7 @end 8 9 @class BWApp;10 @interface BWAppCell : UITableViewCell11 12 @property (nonatomic, strong) BWApp *app;13 @property (nonatomic, strong) id delegate;14 15 @end16 17 //18 // BWAppCell.m19 // IOS_0112_应用管理20 //21 // Created by ma c on 16/1/12.22 // Copyright (c) 2016年 博文科技. All rights reserved.23 //24 25 #import "BWAppCell.h"26 #import "BWApp.h"27 28 @interface BWAppCell ()29 @property (weak, nonatomic) IBOutlet UIImageView *appIcon;30 @property (weak, nonatomic) IBOutlet UILabel *appName;31 @property (weak, nonatomic) IBOutlet UILabel *appDesc;32 33 @property (weak, nonatomic) IBOutlet UIButton *appDownload;34 - (IBAction)appDownload:(id)sender;35 36 @end37 38 @implementation BWAppCell39 40 - (void)setApp:(BWApp *)app41 {42 _app = app;43 44 //给子控件设置数据45 self.appIcon.image = [UIImage imageNamed:_app.icon];46 self.appName.text = _app.name;47 self.appDesc.text = [NSString stringWithFormat:@"大小:%@ | 下载量:%@",_app.size,_app.download];48 49 //更新下载按钮状态50 if (app.isDownloaded) {51 self.appDownload.enabled = NO;52 }53 else54 self.appDownload.enabled = YES;55 56 57 }58 59 60 #pragma mark - 下载按钮点击事件61 - (IBAction)appDownload:(id)sender {62 //1.禁用按钮63 self.appDownload.enabled = NO;64 //已经被点击过了65 self.app.isDownloaded = YES;66 if ([self.delegate respondsToSelector:@selector(btnDownloadClick:)]) {67 [self.delegate btnDownloadClick:self];68 }69 }70 71 - (void)awakeFromNib {72 // Initialization code73 }74 75 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {76 [super setSelected:selected animated:animated];77 78 // Configure the view for the selected state79 }80 81 @end
三、Controller
1 // 2 // ViewController.m 3 // IOS_0112_应用管理 4 // 5 // Created by ma c on 16/1/12. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWApp.h" 11 #import "BWAppCell.h" 12 @interface ViewController ()13 14 @property (nonatomic, strong) NSArray *appArray; 15 16 @end 17 18 @implementation ViewController 19 20 #pragma mark - appCellDelegate代理方法 21 - (void)btnDownloadClick:(BWAppCell *)appCell 22 { 23 //1.创建一个Label 24 UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectMake(140, (self.view.frame.size.height - 37)/2, 95, 37)]; 25 lblMsg.text = @"正在下载..."; 26 lblMsg.textAlignment = NSTextAlignmentCenter; 27 lblMsg.backgroundColor = [UIColor blackColor]; 28 lblMsg.textColor = [UIColor redColor]; 29 //设置透明度 30 lblMsg.alpha = 0.0; 31 //设置圆角 32 lblMsg.layer.cornerRadius = 10; 33 lblMsg.layer.masksToBounds = YES; 34 //[self.view addSubview:lblMsg]; 35 36 [[[UIApplication sharedApplication] keyWindow] addSubview:lblMsg]; 37 38 //动画方式显示Label 39 // [UIView animateWithDuration:1.0 animations:^{ 40 // lblMsg.alpha = 0.6; 41 // }]; 42 43 [UIView animateWithDuration:1.0 animations:^{ 44 lblMsg.alpha = 0.6; 45 } completion:^(BOOL finished) { 46 //动画执行完毕以后 47 //再开启一个新动画 48 [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{ 49 lblMsg.alpha = 0; 50 } completion:^(BOOL finished) { 51 [lblMsg removeFromSuperview]; 52 }]; 53 }]; 54 } 55 56 #pragma mark - 懒加载 57 - (NSArray *)appArray 58 { 59 if (_appArray == nil) { 60 NSString *path = [[NSBundle mainBundle] pathForResource:@"apps_full.plist" ofType:nil]; 61 NSArray *arrDict = [NSArray arrayWithContentsOfFile:path]; 62 NSMutableArray *arrModel = [NSMutableArray array]; 63 64 for (NSDictionary *dict in arrDict) { 65 BWApp *app = [BWApp appWithDict:dict]; 66 [arrModel addObject:app]; 67 } 68 _appArray = arrModel; 69 } 70 return _appArray; 71 } 72 73 #pragma mark - viewDidLoad 74 - (void)viewDidLoad { 75 [super viewDidLoad]; 76 //NSLog(@"%@",self.appArray); 77 self.tableView.rowHeight = 60; 78 } 79 #pragma mark - 数据源方法 80 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 81 { 82 return 1; 83 } 84 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 85 { 86 return self.appArray.count; 87 } 88 89 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 90 { 91 //1.获取数据模型 92 BWApp *model = self.appArray[indexPath.row]; 93 //2.通过storyboard中cell模板创建单元格 94 static NSString *ID = @"app_cell"; 95 BWAppCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 96 //3.设置数据 97 cell.app = model; 98 cell.delegate = self; 99 //4.返回数据100 return cell;101 }102 103 - (void)didReceiveMemoryWarning {104 [super didReceiveMemoryWarning];105 // Dispose of any resources that can be recreated.106 }107 108 @end