Model
//
// YJTg.h
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface YJTg : NSObject
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *buyCount;
- (instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)tgWithDict:(NSDictionary *)dict;
+ (NSMutableArray *)tgs;
@end
//
// YJTg.m
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import "YJTg.h"
@implementation YJTg
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)tgWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
+ (NSMutableArray *)tgs
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
[arrayM addObject:[self tgWithDict:dict]];
}
return arrayM;
}
@end
View
//
// YJTgCell.h
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@class YJTg;
@interface YJTgCell : UITableViewCell
/** 团购的数据模型 */
@property(nonatomic,strong)YJTg *tg;
/** 提供一个类方法,可以快速创建 Cell */
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
//
// YJTgCell.m
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import "YJTgCell.h"
#import "YJTg.h"
@interface YJTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
@implementation YJTgCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
// 1. 可重用标示符
static NSString *ID = @"ID";
// 2. tableView查询可重用Cell
YJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3. 如果没有可重用cell
if (cell ==nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"YJTgCell" owner:nil options:nil] lastObject];
}
return cell;
}
- (void)setTg:(YJTg *)tg
{
// setter方法中,第一句要赋值,否则要在其他方法中使用模型,将无法访问到
_tg = tg;
self.titleLabel.text = tg.title;
self.iconView.image = [UIImage imageNamed:tg.icon];
self.priceLabel.text = tg.price;
self.buyCountLabel.text = tg.buyCount;
}
#pragma mark - 模板提供的方法
/**
初始化方法
使用代码创建Cell的时候会被调用,如果使用XIB或者Storyboard,此方法不会被调用
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
/**
从XIB被加载之后,会自动被调用,如果使用纯代码,不会被执行
*/
- (void)awakeFromNib
{
}
/**
Cell 被选中或者取消选中是都会被调用
如果是自定义Cell控件,所有的子控件都应该添加到contentView中
*/
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//
// YJTgFooterView.h
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@class YJTgFooterView;
@protocol YJTgFooterViewDelegate <NSObject>
/** 视图的下载按钮被点击 */
- (void)tgFooterViewDidDownLoadButtonClick:(YJTgFooterView *)footerView;
@end
@interface YJTgFooterView : UIView
@property (nonatomic,weak)id <YJTgFooterViewDelegate>delegate;
+ (instancetype)footerView;
/** 刷新数据结束后,更新页脚的视图显示 */
- (void)endRefresh;
@end
//
// YJTgFooterView.m
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import "YJTgFooterView.h"
@interface YJTgFooterView()
@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
@property (weak, nonatomic) IBOutlet UIView *tipsView;
@end
@implementation YJTgFooterView
+ (instancetype)footerView
{
return [[[NSBundle mainBundle] loadNibNamed:@"YJTgFooterView" owner:nil options:nil] lastObject];
}
- (IBAction)loadMore
{
//NSLog(@"加载更多");
// 1. 隐藏按钮
self.loadMoreButton.hidden = YES;
// 2. 显示提示视图
self.tipsView.hidden = NO;
// 延时执行命令,多线程GCD
// 今后关于延时的操作,统一使用dispatch_after
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// 块代码中的代码会在1.0秒之后执行
// 3. 加载数据(从网络服务器加载,需要时间...)
// view是用来显示数据的,用代理来实现!
// 代理是用来监听的,发生某些事情的时候,通知"代理"-》控制器去"工作"->加载数据
// 3.1 判断代理是否实现了协议方法
if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownLoadButtonClick:)])
{
[self.delegate tgFooterViewDidDownLoadButtonClick:self];
}
// 4. 加载完成数据
self.loadMoreButton.hidden = NO;
self.tipsView.hidden = YES;
});
}
- (void)endRefresh
{
// 4. 加载完成数据
self.loadMoreButton.hidden = NO;
self.tipsView.hidden = YES;
}
//
// YJTgHeadView.h
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-30.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface YJTgHeadView : UIView
+ (instancetype)hesdView;
@end
//
// YJTgHeadView.m
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-30.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import "YJTgHeadView.h"
#define kImageCount 5
@interface YJTgHeadView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation YJTgHeadView
+ (instancetype)hesdView;
{
return [[[NSBundle mainBundle] loadNibNamed:@"YJTgHeadView" owner:nil options:nil] lastObject];
}
- (void)awakeFromNib
{
// 1.添加图片
CGFloat imageW = self.scrollView.frame.size.width;
CGFloat imageH = self.scrollView.frame.size.height;
CGFloat imageY = 0;
for (int index = 0; index < kImageCount; index++) {
CGFloat imageX = index * imageW;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ad_%02d",index]];
[self.scrollView addSubview:imageView];
}
// 2.设置内容尺寸
self.scrollView.contentSize = CGSizeMake(imageW * kImageCount, 0);
// 取消水平滚动条
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
// 取消弹簧效果
self.scrollView.bounces = NO;
// 设置代理
self.scrollView.delegate = self;
//是否分页
self.scrollView.pagingEnabled = YES;
// 3.分页
self.pageControl.numberOfPages = kImageCount;
// 4.定时器
self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)nextImage
{
// 1.下一页
if (self.pageControl.currentPage == kImageCount - 1) {
self.pageControl.currentPage = 0;
}else{
self.pageControl.currentPage++;
}
// 2.设置滚动
CGPoint offset = CGPointMake(self.scrollView.frame.size.width * self.pageControl.currentPage, 0);
[self.scrollView setContentOffset:offset animated:YES];
}
#pragma mark - 代理方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
[self.timer invalidate];
self.timer = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.timer) return;
self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width;
}
@end
Controller
//
// YJViewController.m
// 01-tableViewCell-团购
//
// Created by JACKY-MAC on 15-6-29.
// Copyright (c) 2015年 www.train.com. All rights reserved.
//
#import "YJViewController.h"
#import "YJTg.h"
#import "YJTgCell.h"
#import "YJTgFooterView.h"
#import "YJTgHeadView.h"
@interface YJViewController ()<YJTgFooterViewDelegate>
@property(nonatomic,strong)NSMutableArray *tgs;
@end
@implementation YJViewController
- (NSArray *)tgs
{
if (_tgs == nil) {
_tgs = [YJTg tgs];
}
return _tgs;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.rowHeight = 80;
// 调整边距,可以让表格视图让开状态栏;
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
// 从XIB加载最后一个视图设置为footerView
// self.tableView.tableFooterView = [[[NSBundle mainBundle] loadNibNamed:@"YJTgFooterView" owner:nil options:nil] lastObject];
YJTgFooterView *footer = [YJTgFooterView footerView];
footer.delegate = self;
self.tableView.tableFooterView = footer;
self.tableView.tableHeaderView = [YJTgHeadView hesdView];
}
#pragma mark - footView的代理方法
- (void)tgFooterViewDidDownLoadButtonClick:(YJTgFooterView *)footerView
{
// 加载数据
NSLog(@"努力加载数据中....");
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// 获得网络数据之后执行的操作
// 向数组中添加数据,模拟网络加载完成,之后的效果
NSDictionary *dict = @{@"title": @"哈哈",@"icon": @"ad_00",@"price": @"100.2",@"buyCount": @"250"};
YJTg *tg = [YJTg tgWithDict:dict];
[self.tgs addObject:tg];
// 刷新数据
// [self.tableView reloadData];
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - 1) inSection:0];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
});
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 创建cell
YJTgCell *cell = [YJTgCell cellWithTableView:tableView];
// 2> 通过数据模型,设置Cell内容,可以让视图控制器不需要了解cell内部的实现细节
cell.tg = self.tgs[indexPath.row];
return cell;
}
@end