今天给同学们带来,通过storyboard自带的cell创建界面并且一个tableView可以绑定和创建不同的cell,那么废话不多说!直接上代码 先看效果图!
//
// ZZApps.h
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZZApps : NSObject
/**
* 大小
*/
@property (nonatomic, copy) NSString *size;
/**
* 下载量
*/
@property (nonatomic, copy) NSString *download;
/**
* 应用名称
*/
@property (nonatomic, copy) NSString *name;
/**
* 应用图标
*/
@property (nonatomic, copy) NSString *icon;
/**
* 已经下载
*/
@property (nonatomic, assign, getter = isDownLoaded) BOOL downLoaded;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
//
// ZZApps.m
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZApps.h"
@implementation ZZApps
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
#warning - KVC直接赋值
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)appWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
//
// ZZAppCell.h
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ZZApps,ZZAppCell;
@protocol ZZAppCellDelegate <NSObject>
@optional
- (void)appCellDidClickDownLoadBtn:(ZZAppCell *)cell;
@end
@interface ZZAppCell : UITableViewCell
@property (nonatomic, strong) ZZApps *app;
@property (nonatomic, weak) id <ZZAppCellDelegate> delegate;
@end
//
// ZZAppCell.m
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZAppCell.h"
#import "ZZApps.h"
@interface ZZAppCell()
@property (weak, nonatomic) IBOutlet UIButton *downLoadBtn;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UILabel *detailView;
- (IBAction)downClick:(UIButton *)button;
@end
@implementation ZZAppCell
- (void)setApp:(ZZApps *)app
{
#warning - 很多人会觉得这个应用左边一个图片然后左上文字左下文字 那么用系统自带的subtitle样式就行那么我们就去storyboard中看看
_app = app;
self.iconView.image = [UIImage imageNamed:app.icon];
self.titleView.text = app.name;
self.detailView.text = [NSString stringWithFormat:@"大小:%@ | 下载量:%@", app.size, app.download];
#warning - 这里解决重用的问题(这里好好去理解,根据模型的值判断)
self.downLoadBtn.enabled = (NO == self.app.isDownLoaded);
}
- (IBAction)downClick:(UIButton *)button {
// 1.通知代理
if ([self.delegate respondsToSelector:@selector(appCellDidClickDownLoadBtn:)]) {
[self.delegate appCellDidClickDownLoadBtn:self];
}
// 2.点击按钮了要设置不能被点击
button.enabled = NO;
self.app.downLoaded = YES;
}
@end
//
// ZZAppTableViewController.h
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZZAppTableViewController : UITableViewController
@end
//
// ZZAppTableViewController.m
// 13-应用管理
//
// Created by 周昭 on 16/12/13.
// Copyright © 2016年 HT_Technology. All rights reserved.
//
#import "ZZAppTableViewController.h"
#import "ZZApps.h"
#import "ZZAppCell.h"
@interface ZZAppTableViewController()<ZZAppCellDelegate>
@property (nonatomic, strong) NSArray *apps;
@end
@implementation ZZAppTableViewController
- (NSArray *)apps
{
if (_apps == nil) {
// 1.获得plist的全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"apps_full.plist" ofType:nil];
// 2.加载数组
NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
// 3.将dictArr中所有的字典转成模型对象,放到新的数组中
NSMutableArray *arr = [NSMutableArray array];
for (NSDictionary *dict in dictArr) {
// 3.1创建模型对象
ZZApps *app = [ZZApps appWithDict:dict];
// 3.2添加模型对象到数组中
[arr addObject:app];
}
// 4.赋值
_apps = arr;
}
return _apps;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.apps.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#warning - 完全需求驱动开发先写好内部最基层的模型然后如何传递如何使用我们就来实现
// 1.创建cell
ZZAppCell *cell = [tableView dequeueReusableCellWithIdentifier:@"app"];
cell.delegate = self;
// 这一步不需要写为什么?我们用的是通过storyboard来创建cell那么他的流程是
// 1:先通过标识去缓存池中取
// 2:如果没有那么他就去storyboard中通过这个标识找如果有就照着这个模子刻出来
//
// if (cell == nil) {
// cell = [[ZZAppCell alloc] init];
// }
// 2.给cell传递模型
cell.app = self.apps[indexPath.row];
// 3.返回cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark ZZAppCellDelegate 方法
- (void)appCellDidClickDownLoadBtn:(ZZAppCell *)cell
{
// 1.添加标签
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"成功下载%@",cell.app.name];
label.font = [UIFont systemFontOfSize:13.0f];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor blackColor];
label.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
label.bounds = CGRectMake(0, 0, 150, 35);
label.alpha = 0.0;
label.layer.cornerRadius = 5;
label.clipsToBounds = YES;
[self.view addSubview:label];
// 2.动画
[UIView animateWithDuration:0.5 animations:^{
label.alpha = 0.5;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
[label removeFromSuperview];
}];
}];
}
@end