#import "ViewController.h"
#import "MJRefresh.h"//包含头文件
#import "MyHeader.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableV;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,assign) int page;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.page = 1;
[self loadNewData];
[self createTableView];
// [self addDownRresh];
//
// [self addUpMore];
// [self addDownGifFresh];
// [self addUpMoreGif];
[self addUpBack];
[self addDownCustom];
}
- (void)addDownCustom{
MyHeader *header = [MyHeader headerWithRefreshingBlock:^{
[self loadNewData];
}];
header.state = MJRefreshStateRefreshing;
self.tableV.mj_header = header;
}
- (void)addUpBack{
MJRefreshBackFooter *footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
[self loadMoreData];
}];
self.tableV.mj_footer = footer;
}
- (void)addDownGifFresh{
//MJRefreshGifHeader添加动画图片的下拉刷新
MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//下拉刷新会触发这个block
//请求最新的数据
[self loadNewData];
}];
//闲置状态显示的图片
[header setImages:@[[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStateIdle];
//正在刷新状态的动画
[header setImages:@[[UIImage imageNamed:@"common_loading_anne_0"],[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStateRefreshing];
//即将刷新状态的图片
[header setImages:@[[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStatePulling];
self.tableV.mj_header = header;
}
- (void)addUpMoreGif{
MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
[self loadMoreData];
}];
//闲置状态显示的图片
[footer setImages:@[[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStateIdle];
//正在刷新状态的动画
[footer setImages:@[[UIImage imageNamed:@"common_loading_anne_0"],[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStateRefreshing];
//即将刷新状态的图片
[footer setImages:@[[UIImage imageNamed:@"common_loading_anne_1"]] forState:MJRefreshStatePulling];
self.tableV.mj_footer = footer;
}
//添加下拉刷新
- (void)addDownRresh{
//MJRefreshNormalHeader普通样式的下拉刷新
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//显示状态的label
header.stateLabel.hidden = NO;
//显示最后更新时间的label
header.lastUpdatedTimeLabel.hidden = NO;
header.stateLabel.font = [UIFont systemFontOfSize:22];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
header.stateLabel.textColor = [UIColor redColor];
header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
[header setTitle:@"赶紧拽啊" forState:MJRefreshStateIdle];
[header setTitle:@"我要炸了" forState:MJRefreshStateRefreshing];
/*
// 普通闲置状态
MJRefreshStateIdle = 1,
/// 松开就可以进行刷新的状态
MJRefreshStatePulling,
// 正在刷新中的状态
MJRefreshStateRefreshing,
// 即将刷新的状态
MJRefreshStateWillRefresh,
// 所有数据加载完毕,没有更多的数据了
MJRefreshStateNoMoreData
*/
//开始刷新
[header beginRefreshing];
//把下拉刷新添加到tableview上
self.tableV.mj_header = header;
}
//添加上拉加载
- (void)addUpMore{
//MJRefreshAutoNormalFooter普通样式的上拉加载
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//把上拉记载添加到tableview上
self.tableV.mj_footer = footer;
}
- (void)createTableView{
self.tableV = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableV.delegate = self;
self.tableV.dataSource = self;
[self.view addSubview:self.tableV];
}
- (NSMutableArray *)dataArr{
if (!_dataArr) {
_dataArr = [[NSMutableArray alloc]init];
}
return _dataArr;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid = @"id";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = self.dataArr[indexPath.row];
return cell;
}
//请求数据
- (void)loadNewData{
self.page = 1;
sleep(2);
//清空数据源
[self.dataArr removeAllObjects];
for (int i = 0 ;i < 5 ;i++) {
[self.dataArr addObject:[NSString stringWithFormat:@"随机假数据-%d",arc4random()%10000000]];
}
//下载结束后 刷新tableview
[self.tableV reloadData];
//请求结束 结束刷新
[self.tableV.mj_header endRefreshing];
}
//下载更多数据
- (void)loadMoreData{
self.page++;
//请求下一页的数据
for (int i = 0; i < 5; i++) {
[self.dataArr addObject:[NSString stringWithFormat:@"随机假数据-%d",arc4random()%10]];
}
NSString *path = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%d",self.page];
// http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%d,self.page
if (self.page > 20) {
//提示 没有更多数据了
self.tableV.mj_footer.hidden = YES;
}else{
[self.tableV reloadData];
[self.tableV.mj_footer endRefreshing];
}
}
@end
#import "MyHeader.h"
@implementation MyHeader
{
UIProgressView *_pro;
UISegmentedControl *_seg;
}
- (void)prepare{
[super prepare];
//在下拉刷新上添加子控件
_pro = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
// pro.frame = self.bounds;
// [self addSubview:_pro];
_seg = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
_seg.selectedSegmentIndex = 0;
[self addSubview:_seg];
}
- (void)placeSubviews{
[super placeSubviews];
//设置子控件的位置
// _pro.frame = self.bounds;
_seg.frame = self.bounds;
}
//footer header的状态
- (void)setState:(MJRefreshState)state{
if (state == MJRefreshStateRefreshing) {
_seg.selectedSegmentIndex = 1;
}
}
@end