MJRefresh 是一個優(yōu)秀的下拉刷新組件。
github 地址
//
// ViewController.m
// mjtable
//
// Created by ldhonline on 2018/10/26.
// Copyright ? 2018年 aidoutu.com. All rights reserved.
//
#import "ViewController.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "MASConstraint+Hidden.h"
#import "UIView+MasonryHidden.h"
#import "Color+Utils.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation ViewController
{
UITableView *_table;
NSUInteger count;
NSArray *data;
}
- (void)viewDidLoad {
[super viewDidLoad];
count = 0;
data = @[];
_table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 400, 400) style:UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:@"r"];
img.contentMode = UIViewContentModeScaleAspectFit;
img.frame = CGRectMake(0,-50, 400, 50);
[_table addSubview:img];
// 加入內(nèi)部 padding
_table.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
// [_table reloadData];
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[self performSelector:@selector(loadTableViewFirstPage) withObject:nil afterDelay:0.5];
}];
// 當table 上面插入了其它元素時慢哈,忽略多少 paddingTop
header.ignoredScrollViewContentInsetTop = 50;
// auto 模式會自動跟在最后的元素后面
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
[self performSelector:@selector(loadTableViewNextPage) withObject:nil afterDelay:0.5];
}];
footer.hidden = YES;// 開始是隱藏的
footer.onlyRefreshPerDrag = YES; // 多次拉取也只加載一次
footer.triggerAutomaticallyRefreshPercent = -20;// 離 bottom 多遠的時候就觸發(fā)
[footer setTitle:@"下拉查看更多的評論" forState:MJRefreshStateIdle];
[footer setTitle:@"正在瘋狂加載 ..." forState:MJRefreshStateRefreshing];
[footer setTitle:@"我也是有底線" forState:MJRefreshStateNoMoreData];
_table.mj_header = header;
_table.mj_footer = footer;
[self loadTableViewFirstPage];
}
-(void)mj_firstPageDidLoad{
[_table.mj_header endRefreshing];
// 只要有數(shù)據(jù)椭懊,就顯示加載更多, 沒有數(shù)據(jù)隱藏控件对粪,不能再上拉
_table.mj_footer.hidden = [data count] == 0;
// 不足一屏的時候顯示沒有更多了,也不能上拉
if([data count] < 5){
[_table.mj_footer endRefreshingWithNoMoreData];
}else{
[_table.mj_footer resetNoMoreData];
}
[_table reloadData];
}
-(void)mj_nextPageDidLoad{
// 加載一頁后回到等待上拉狀態(tài)
[_table.mj_footer endRefreshing];
[_table reloadData];
}
-(void)mj_allPageDidLoad{
// 沒有更多數(shù)據(jù)時,回到不能上拉狀態(tài)
[_table.mj_footer endRefreshingWithNoMoreData];
[_table reloadData];
}
- (void)loadTableViewFirstPage{
data = [self getArrWithCount:5];
[self mj_firstPageDidLoad];
}
- (void)loadTableViewNextPage{
data = [data arrayByAddingObjectsFromArray:[self getArrWithCount:5]];
if([data count] >= 45){
[self mj_allPageDidLoad];
}else{
[self mj_nextPageDidLoad];
}
}
- (NSArray *)getArrWithCount:(NSUInteger)count{
NSMutableArray *ret = [NSMutableArray array];
for (int i = 0; i < count; i++) {
[ret addObject: randColor];
}
return ret;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [UITableViewCell new];
cell.textLabel.text = @"ok, is google";
cell.backgroundColor = data[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
@end