MJRefresh 是一个优秀的下拉刷新组件。
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];
// 加入内部 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 多远的时候就触发
[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];
// 只要有数据,就显示加载更多, 没有数据隐藏控件,不能再上拉
_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{
// 加载一页后回到等待上拉状态
[_table.mj_footer endRefreshing];
[_table reloadData];
}
-(void)mj_allPageDidLoad{
// 没有更多数据时,回到不能上拉状态
[_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
网友评论