MJRefresh-简单使用
#import <MJRefresh.h> @interface HKTopicViewController () /** 当前最后一条帖子数据的描述信息,专门用来加载下一页数据 */ @property (nonatomic, copy) NSString *maxtime; /** 所有的帖子数据 */ @property (nonatomic, strong) NSMutableArray<HKTopic *> *topics; /** 请求管理者 */ @property (nonatomic, strong) AFHTTPSessionManager *manager; @end - (AFHTTPSessionManager *)manager { if (!_manager) { _manager = [AFHTTPSessionManager manager]; } return _manager; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = RGB(206, 206, 206); self.tableView.contentInset = UIEdgeInsetsMake(HKNavMaxY + HKTitlesViewH, 0, HKTabBarH, 0); self.tableView.scrollIndicatorInsets = self.tableView.contentInset; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 注册cell UINib *nib = [UINib nibWithNibName:NSStringFromClass([HKTopicCell class]) bundle:nil]; [self.tableView registerNib:nib forCellReuseIdentifier:HKTopicCellId]; [self setupRefresh]; } - (void)setupRefresh { // 广告条 UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor blackColor]; label.frame = CGRectMake(0, 0, 0, 30); label.textColor = [UIColor whiteColor]; label.text = @"广告"; label.textAlignment = NSTextAlignmentCenter; self.tableView.tableHeaderView = label; // header self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)]; // 自动切换透明度 self.tableView.mj_header.automaticallyChangeAlpha = YES; [self.tableView.mj_header beginRefreshing]; // footer self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)]; } #pragma mark - 数据处理 /** * 发送请求给服务器,下拉刷新数据 */ - (void)loadNewTopics { // 1.取消之前的请求 [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)]; // 2.拼接参数 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; parameters[@"a"] = @"list"; parameters[@"c"] = @"data"; parameters[@"type"] = @(self.type); // 3.发送请求 [self.manager GET:HKCommonURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 存储maxtime self.maxtime = responseObject[@"info"][@"maxtime"]; // 字典数组 -> 模型数据 self.topics = [HKTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]; // 刷新表格 [self.tableView reloadData]; // 结束刷新 [self.tableView.mj_header endRefreshing]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的error,其他网络问题导致的error [SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"]; } // 结束刷新 [self.tableView.mj_header endRefreshing]; }]; } /** * 发送请求给服务器,上拉加载更多数据 */ - (void)loadMoreTopics { // 1.取消之前的请求 [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)]; // 2.拼接参数 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; parameters[@"a"] = @"list"; parameters[@"c"] = @"data"; parameters[@"type"] = @(self.type); parameters[@"maxtime"] = self.maxtime; // 3.发送请求 [self.manager GET:HKCommonURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 存储maxtime self.maxtime = responseObject[@"info"][@"maxtime"]; // 字典数组 -> 模型数据 NSArray *moreTopics = [HKTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]; // 累加到旧数组的后面 [self.topics addObjectsFromArray:moreTopics]; // 刷新表格 [self.tableView reloadData]; // 结束刷新 [self.tableView.mj_footer endRefreshing]; if (self.topics.count >= 60) { [self.tableView.mj_footer endRefreshingWithNoMoreData]; } else { [self.tableView.mj_footer endRefreshing]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的error,其他网络问题导致的error [SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"]; } // 结束刷新 [self.tableView.mj_footer endRefreshing]; }]; } #pragma mark - 数据源 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 根据数据量显示或者隐藏footer self.tableView.mj_footer.hidden = (self.topics.count == 0); return self.topics.count; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 清除内存缓存 [[SDImageCache sharedImageCache] clearMemory]; }
刷新控件的状态:
/** 刷新控件的状态 */ typedef NS_ENUM(NSInteger, MJRefreshState) { /** 普通闲置状态 */ MJRefreshStateIdle = 1, /** 松开就可以进行刷新的状态 */ MJRefreshStatePulling, /** 正在刷新中的状态 */ MJRefreshStateRefreshing, /** 即将刷新的状态 */ MJRefreshStateWillRefresh, /** 所有数据加载完毕,没有更多的数据了 */ MJRefreshStateNoMoreData };
①(局部自定义)设置状态文字颜色-自动切换透明度
自定义一个类HKRefreshHeader继承于:MJRefreshNormalHeader
#import "HKRefreshHeader.h" @implementation HKRefreshHeader - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 设置状态文字颜色 self.stateLabel.textColor = [UIColor blueColor]; self.stateLabel.font = [UIFont systemFontOfSize:17]; [self setTitle:@"赶紧下拉刷新" forState:MJRefreshStateIdle]; [self setTitle:@"松开?上刷新" forState:MJRefreshStatePulling]; [self setTitle:@"正在拼命刷新..." forState:MJRefreshStateRefreshing]; // 隐藏时间 self.lastUpdatedTimeLabel.hidden = YES; // 自动切换透明度 self.automaticallyChangeAlpha = YES; } return self; } @end
②(全部自定义)
自定义一个类HKCustomRefreshHeader继承于:MJRefreshHeader
#import "HKCustomRefreshHeader.h" @interface HKCustomRefreshHeader() /** 开关 */ @property (nonatomic, weak) UISwitch *s; /** logo */ @property (nonatomic, weak) UIImageView *logo; @end @implementation HKCustomRefreshHeader - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { UISwitch *s = [[UISwitch alloc] init]; [self addSubview:s]; self.s = s; UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]]; [self addSubview:logo]; self.logo = logo; //self.height = 70;//修改高度 } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.logo.centerX = self.width * 0.5; self.logo.y = - 3 * self.logo.height; self.s.centerX = self.width * 0.5; self.s.centerY = self.height * 0.5; } #pragma mark - 重写Header内部的方法 - (void)setState:(MJRefreshState)state { [super setState:state]; if (state == MJRefreshStateIdle) { // 下拉可以刷新 [self.s setOn:NO animated:YES]; [UIView animateWithDuration:0.25 animations:^{ self.s.transform = CGAffineTransformIdentity; }]; } else if (state == MJRefreshStatePulling) { // 松开立即刷新 [self.s setOn:YES animated:YES]; [UIView animateWithDuration:0.25 animations:^{ self.s.transform = CGAffineTransformMakeRotation(M_PI_2); }]; } else if (state == MJRefreshStateRefreshing) { // 正在刷新 [self.s setOn:YES animated:YES]; [UIView animateWithDuration:0.25 animations:^{ self.s.transform = CGAffineTransformMakeRotation(M_PI_2); }]; } } @end
使用:
// header self.tableView.mj_header = [HKRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
补充:
MJRefresh 下拉刷新头部不显示的问题
在uicollectionView 头部有偏移量的时候, 往往给其加下拉刷新的的时候 , 头部的字(mj默认的)往往显示不出来, 此时就需要
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ }]; header.ignoredScrollViewContentInsetTop = 20; self.collectionView.mj_header = header;