效果图:
Model:
//
// QJTg.h
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import <Foundation/Foundation.h>
@interface QJTg : NSObject
@property (nonatomic , copy)NSString * icon;
@property (nonatomic , copy)NSString * price;
@property (nonatomic , copy)NSString * title;
@property (nonatomic , copy)NSString * buyCount;
+ tgWithDictionary:(NSDictionary *)dic;
- initWithDictionary:(NSDictionary *)dic;
@end
//
// QJTg.m
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import "QJTg.h"
@implementation QJTg
+ tgWithDictionary:(NSDictionary *)dic{
return [[self alloc]initWithDictionary:dic];
}
- initWithDictionary:(NSDictionary *)dic{
if (self = [super init]) {
self.icon = dic[@"icon"];
self.price = dic[@"price"];
self.title = dic[@"title"];
self.buyCount = dic[@"buyCount"];
}
return self;
}
@end
都是从XIB中获取视图框架
//
// TgTableViewCell.h
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import <UIKit/UIKit.h>
@class QJTg ;
@interface TgTableViewCell : UITableViewCell
@property (nonatomic , weak)QJTg * tg;
+ (instancetype)tgTableViewCellWithTableView:(UITableView *) tableView;
@end
//
// TgTableViewCell.m
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import "TgTableViewCell.h"
#import "QJTg.h"
@interface TgTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *tgIcon;
@property (weak, nonatomic) IBOutlet UILabel *tgTitle;
@property (weak, nonatomic) IBOutlet UILabel *tgPrice;
@property (weak, nonatomic) IBOutlet UILabel *tgBuyCount;
@end
@implementation TgTableViewCell
+ (instancetype)tgTableViewCellWithTableView:(UITableView *) tableView{
NSString * ID = @"tg";
TgTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"TgTableViewCell" owner:nil options:nil] lastObject];
}
return cell;
}
- (void)setTg:(QJTg *)tg{
_tg = tg;
self.tgIcon.image = [UIImage imageNamed:tg.icon];
self.tgTitle.text = tg.title;
self.tgPrice.text = tg.price;
self.tgBuyCount.text = tg.buyCount ;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
//
// QJFootView.h
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import <UIKit/UIKit.h>
@class QJFootView;
@protocol QJFootViewDelegate <NSObject>
@optional
- (void)footViewDidFootBtn:(QJFootView *)footView;
@end
@interface QJFootView : UIView
@property (nonatomic , weak)id<QJFootViewDelegate> delegate;
+ (instancetype)footView;
@end
//
// QJFootView.m
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
// Copyright © 2015年 itcast. All rights reserved.
//
#import "QJFootView.h"
@interface QJFootView()
@property (weak, nonatomic) IBOutlet UIButton *footBtn;
@property (weak, nonatomic) IBOutlet UIView *footView;
- (IBAction)addMore;
@end
@implementation QJFootView
+ (instancetype)footView{
return [[[NSBundle mainBundle]loadNibNamed:@"QJFootView" owner:nil options:nil] lastObject];
}
/**
* 添加更多的食品显示在手机屏幕上
*/
- (IBAction)addMore {
self.footBtn.hidden = YES ;
self.footView.hidden = NO ;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 1.5秒后执行下面的代码
if ([self.delegate respondsToSelector:@selector(footViewDidFootBtn:)]) {
[self.delegate footViewDidFootBtn:self];
}
self.footBtn.hidden = NO;
self.footView.hidden = YES ;
});
}
- (void)awakeFromNib{
self.footBtn.hidden = NO;
self.footView.hidden = YES ;
}
@end
//
// QJHeaderView.h
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import <UIKit/UIKit.h>
@interface QJHeaderView : UIView
+ (instancetype)headerView;
@end
//
// QJHeaderView.m
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
// Copyright © 2015年 itcast. All rights reserved.
//
#import "QJHeaderView.h"
@interface QJHeaderView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageContol;
- (void)nextAdImg;
@end
@implementation QJHeaderView
/**
* 获取QJHeaderView.xib中的view
*/
+ (instancetype)headerView{
return [[[NSBundle mainBundle]loadNibNamed:@"QJHeaderView" owner:nil options:nil] lastObject];
}
/**
* 定时器每隔一段时间就会调用一次
*/
- (void)nextAdImg{
if(self.pageContol.numberOfPages - 1 == self.pageContol.currentPage )
self.pageContol.currentPage = 0;
else
self.pageContol.currentPage++;
CGFloat imgViewW = self.scrollView.frame.size.width ;
CGFloat imgViewH = 0;
self.scrollView.contentOffset = CGPointMake(self.pageContol.currentPage * imgViewW, imgViewH);
}
/**
* 当xib被初始化完毕时调用一次
*/
- (void)awakeFromNib{
int numberOfAd = 5;
CGFloat imgViewW = 414;
CGFloat imgViewH = 220;
// 设置scrollView
for (int i = 0; i < numberOfAd; i++) {
UIImageView * imgView = [[UIImageView alloc]init];
CGFloat imgViewX = i*imgViewW;
CGFloat imgViewY = 0;
imgView.frame = CGRectMake(imgViewX , imgViewY , imgViewW, imgViewH);
imgView.image =[UIImage imageNamed:[NSString stringWithFormat:@"ad_%02d",i]];
[self.scrollView addSubview:imgView];
}
// 设置代理
self.scrollView.delegate = self;
// 一定要设置动能够滚动的范围
self.scrollView.contentSize = CGSizeMake(imgViewW * numberOfAd , imgViewH);
self.scrollView.scrollEnabled = YES ;
self.scrollView.showsHorizontalScrollIndicator = NO;
// 设置用户能否与scrollView交互
self.scrollView.userInteractionEnabled = NO;
// 设置pageContol
self.pageContol.numberOfPages = numberOfAd ;
self.pageContol.currentPageIndicatorTintColor = [UIColor redColor];
self.pageContol.pageIndicatorTintColor = [UIColor blueColor];
//限制用户交互
self.pageContol.userInteractionEnabled = NO;
// 设置定时器
// [NSTimer timerWithTimeInterval:1.0 target:self selector: userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextAdImg) userInfo:nil repeats:YES];
}
#pragma mark - UIScrollViewDelegate协议方法
/**
* 当scrollView正在被拖动时调用
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint point = self.scrollView.contentOffset;
int page = (point.x + self.scrollView.frame.size.width * 0.5) /self.scrollView.frame.size.width;
self.pageContol.currentPage = page;
}
@end
Controller:
//
// ViewController.h
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// 08-团购
//
// Created by 瞿杰 on 15/9/29.
//
#import "ViewController.h"
#import "QJTg.h"
#import "TgTableViewCell.h"
#import "QJFootView.h"
#import "QJHeaderView.h"
@interface ViewController ()<UITableViewDataSource , QJFootViewDelegate>
@property (nonatomic,strong)NSMutableArray * tgs;
@property (nonatomic,weak)IBOutlet UITableView * tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源
// self.tableView.dataSource = self;
// 设置headerView
QJHeaderView * headerView = [QJHeaderView headerView];
self.tableView.tableHeaderView = headerView ;
QJFootView * footView = [QJFootView footView];
// 设置footView的代理
footView.delegate = self ;
self.tableView.tableFooterView = footView ;
}
- (BOOL)prefersStatusBarHidden{
return YES ;
}
- (NSMutableArray *)tgs{
// 赖加载数据
if (nil == _tgs) {
NSString * path = [[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
NSArray * tgsArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray * tgs = [NSMutableArray array];
for(NSDictionary * dic in tgsArr){
QJTg * tg = [QJTg tgWithDictionary:dic];
[tgs addObject:tg];
}
_tgs = tgs;
}
return _tgs;
}
#pragma mark -QJFootViewDelegate中的方法
/**
* 当点击了footerView时就会调用代理的当前方法
*/
- (void)footViewDidFootBtn:(QJFootView *)footView{
NSString * path = [[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
NSArray * tgsArr = [NSArray arrayWithContentsOfFile:path];
for(NSDictionary * dic in tgsArr)
{
QJTg * tg = [QJTg tgWithDictionary:dic];
[self.tgs addObject:tg];
break;
}
//重新加载数据,调用数据源协议方法
[self.tableView reloadData];
}
#pragma mark - 数据源协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TgTableViewCell * cell = [TgTableViewCell tgTableViewCellWithTableView:tableView];
QJTg * tg = self.tgs[indexPath.row];
cell.tg = tg;
return cell;
}
@end