一、Model
//
// BWApp.h
// IOS_0112_应用管理
//
// Created by ma c on 16/1/12.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import <Foundation/Foundation.h> @interface BWApp : NSObject @property (nonatomic, copy) NSString *size;
@property (nonatomic, copy) NSString *download;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
//标记是否被下载过
@property (nonatomic, assign) BOOL isDownloaded; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict; @end //
// BWApp.m
// IOS_0112_应用管理
//
// Created by ma c on 16/1/12.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "BWApp.h" @implementation BWApp - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)appWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end
二、View
#import <UIKit/UIKit.h>
@class BWAppCell;
@protocol appCellDelegate <NSObject> - (void)btnDownloadClick:(BWAppCell *)appCell; @end @class BWApp;
@interface BWAppCell : UITableViewCell @property (nonatomic, strong) BWApp *app;
@property (nonatomic, strong) id<appCellDelegate> delegate; @end //
// BWAppCell.m
// IOS_0112_应用管理
//
// Created by ma c on 16/1/12.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "BWAppCell.h"
#import "BWApp.h" @interface BWAppCell ()
@property (weak, nonatomic) IBOutlet UIImageView *appIcon;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (weak, nonatomic) IBOutlet UILabel *appDesc; @property (weak, nonatomic) IBOutlet UIButton *appDownload;
- (IBAction)appDownload:(id)sender; @end @implementation BWAppCell - (void)setApp:(BWApp *)app
{
_app = app; //给子控件设置数据
self.appIcon.image = [UIImage imageNamed:_app.icon];
self.appName.text = _app.name;
self.appDesc.text = [NSString stringWithFormat:@"大小:%@ | 下载量:%@",_app.size,_app.download]; //更新下载按钮状态
if (app.isDownloaded) {
self.appDownload.enabled = NO;
}
else
self.appDownload.enabled = YES; } #pragma mark - 下载按钮点击事件
- (IBAction)appDownload:(id)sender {
//1.禁用按钮
self.appDownload.enabled = NO;
//已经被点击过了
self.app.isDownloaded = YES;
if ([self.delegate respondsToSelector:@selector(btnDownloadClick:)]) {
[self.delegate btnDownloadClick:self];
}
} - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end
三、Controller
//
// ViewController.m
// IOS_0112_应用管理
//
// Created by ma c on 16/1/12.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "BWApp.h"
#import "BWAppCell.h"
@interface ViewController ()<appCellDelegate> @property (nonatomic, strong) NSArray *appArray; @end @implementation ViewController #pragma mark - appCellDelegate代理方法
- (void)btnDownloadClick:(BWAppCell *)appCell
{
//1.创建一个Label
UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectMake(, (self.view.frame.size.height - )/, , )];
lblMsg.text = @"正在下载...";
lblMsg.textAlignment = NSTextAlignmentCenter;
lblMsg.backgroundColor = [UIColor blackColor];
lblMsg.textColor = [UIColor redColor];
//设置透明度
lblMsg.alpha = 0.0;
//设置圆角
lblMsg.layer.cornerRadius = ;
lblMsg.layer.masksToBounds = YES;
//[self.view addSubview:lblMsg]; [[[UIApplication sharedApplication] keyWindow] addSubview:lblMsg]; //动画方式显示Label
// [UIView animateWithDuration:1.0 animations:^{
// lblMsg.alpha = 0.6;
// }]; [UIView animateWithDuration:1.0 animations:^{
lblMsg.alpha = 0.6;
} completion:^(BOOL finished) {
//动画执行完毕以后
//再开启一个新动画
[UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
lblMsg.alpha = ;
} completion:^(BOOL finished) {
[lblMsg removeFromSuperview];
}];
}];
} #pragma mark - 懒加载
- (NSArray *)appArray
{
if (_appArray == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"apps_full.plist" ofType:nil];
NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrModel = [NSMutableArray array]; for (NSDictionary *dict in arrDict) {
BWApp *app = [BWApp appWithDict:dict];
[arrModel addObject:app];
}
_appArray = arrModel;
}
return _appArray;
} #pragma mark - viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
//NSLog(@"%@",self.appArray);
self.tableView.rowHeight = ;
}
#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.appArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.获取数据模型
BWApp *model = self.appArray[indexPath.row];
//2.通过storyboard中cell模板创建单元格
static NSString *ID = @"app_cell";
BWAppCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//3.设置数据
cell.app = model;
cell.delegate = self;
//4.返回数据
return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end