MJRefresh实现刷新(使用它的Block方法)
方法 实现 刷新 Block 使用
2023-09-27 14:28:40 时间
MJRefresh实现刷新(使用它的Block方法)
//
// YFMVCPostListViewController.m
// iOS122
//
// Created by 颜风 on 15/10/14.
// Copyright (c) 2015年 iOS122. All rights reserved.
//
#import "YFMVCPostListViewController.h"
#import "YFArticleModel.h"
#import <AFNetworking.h>
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "YFMVCPostViewController.h"
@interface YFMVCPostListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * articles; //!< 文章数组,内部存储AFArticleModel类型.
@property (assign, nonatomic) NSInteger page; //!< 数据页数.表示下次请求第几页的数据.
@end
@implementation YFMVCPostListViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSMutableArray *)articles
{
if (nil == _articles) {
_articles = [NSMutableArray arrayWithCapacity: 42];
}
return _articles;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
// 马上进入刷新状态
[self.tableView.header beginRefreshing];
}
- (UITableView *)tableView
{
if (nil == _tableView) {
_tableView = [[UITableView alloc] init];
[self.view addSubview: _tableView];
[_tableView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
_tableView.delegate = self;
_tableView.dataSource = self;
NSString * cellReuseIdentifier = NSStringFromClass([UITableViewCell class]);
[_tableView registerClass: NSClassFromString(cellReuseIdentifier) forCellReuseIdentifier:cellReuseIdentifier];
_tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
self.page = 0;
[self updateData];
}];
_tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
[self updateData];
}];
}
return _tableView;
}
/**
* 更新视图.
*/
- (void) updateView
{
[self.tableView reloadData];
}
/**
* 停止刷新
*/
-(void)endRefresh{
[self.tableView.header endRefreshing];
[self.tableView.footer endRefreshing];
}
/**
* 更新数据.
*
* 数据更新后,会自动更新视图.
*/
- (void)updateData
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString * urlStr = [NSString stringWithFormat: @"http://www.ios122.com/find_php/index.php?viewController=YFPostListViewController&model[category]=%@&model[page]=%ld", self.categoryName, (long)self.page ++];
[manager GET: urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self endRefresh];
if (1 == self.page) { // 说明是在重新请求数据.
self.articles = nil;
}
NSArray * responseArticles = [YFArticleModel objectArrayWithKeyValuesArray: responseObject];
[self.articles addObjectsFromArray: responseArticles];
[self updateView];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self endRefresh];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"您的网络不给力!";
[hud hide: YES afterDelay: 2];
}];
}
# pragma mark - tabelView代理方法.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger number = self.articles.count;
return number;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellReuseIdentifier = NSStringFromClass([UITableViewCell class]);
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellReuseIdentifier forIndexPath:indexPath];
YFArticleModel * model = self.articles[indexPath.row];
NSString * content = [NSString stringWithFormat: @"标题:%@ 内容:%@", model.title, model.desc];
cell.textLabel.text = content;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 跳转到博客详情.
YFArticleModel * articleModel = self.articles[indexPath.row];
YFMVCPostViewController * postVC = [[YFMVCPostViewController alloc] init];
postVC.articleID = articleModel.id;
[self.navigationController pushViewController: postVC animated: YES];
}
@end
相关文章
- 关于编写有效测试用例的思考和方法
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
- 在启动php时,无法启动此程序,由于计算机中丢失MSVCR110.dll的解决方法
- Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)
- 半阈值化的原理、作用及利用函数OpenCV的函数threshold()实现半阈值化的方法
- 干货 | 便捷、可靠的数据传输方法
- 运维前线:一线运维专家的运维方法、技巧与实践1.6 运维自动化系统的实现
- 微信公众号点击菜单即可打开并登录微站的实现方法
- 坑——java的String.split()方法
- JS实现关闭当前子窗口,刷新父窗口及调用父窗口的方法
- 【Java】查找 List/String字符串 中是否包含指定的 元素/字串 contains方法实现
- 深度学习模型参数初始化的方法
- 《Python面向对象编程指南》——1.6 在每个子类中实现__init()__方法
- 保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护
- Dynamic CRM 2013学习笔记(十七)JS读写各种类型字段方法及技巧
- PyTorch多卡分布式训练DistributedDataParallel 使用方法
- Excel-数据分列的多种方法实现
- 单片机-结构体函数指针高级使用方法
- Fragment的setUserVisibleHint方法实现懒加载
- [Oracle]Master表字段扩张时的对应方法
- 五种方法教你搭建Linux学习环境(三)
- 《Java设计模式》之模板方法模式
- 实现财务自由 之 A 股上市公司股票同行业的排名情况(例如贵州茅台所属行业总市值排名)简单查看方法
- C语言 实现两种排序方法
- 安卓开发笔记(十四):点击按钮直接拨号,不需要在拨号界面再点击一次的方法
- 【图像处理】——图像滤波(Python+opencv实现三种方法:均值滤波、中值滤波、高斯滤波等)