MJRefresh实现刷新(使用它的Block方法)
2024-08-30 15:23:06
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# -- 学习笔记(二)
- CPU制造工艺 级选来决定cpu等级
- SQL千万级数据设计和优化
- 动态代理CGlib实例
- 【STL学习】智能指针之weak_ptr
- [Bootstrap] 6. Navigation
- Yii PHP 框架分析(四)
- [POJ] 3264 Balanced Lineup [线段树]
- POJ 3616 Milking Time DP题解
- 一个Dotnet数据框架的bug
- vue2.0在android5.0白屏, es6转es5保证浏览器兼容性
- sql中的exists用法
- 微信小程序之地理位置授权 wx.getLocation
- PHP的线性安全和非线性安全的区别
- mount /dev/sr0 /media/cdrom you must specify the filesystem type
- 18、Java线程面试题 Top 50
- 单端通用ISM频段接收器 Si4313
- Redis事务为什么不支持回滚
- 安装spring报错:Cannot complete the install because of a conflicting dependency.
- C# 和 Java的区别积累
热门文章
- 遇到的django问题
- es6(三set和map数据结构)
- 哈理工(HUST)第八届程序设计竞赛--小乐乐的组合数
- 关于U盘安装ubuntu-18.04安装时候出现的grub-efi-amd64-signed的问题。
- MindManager 设置默认Note字体大小
- php 正则匹配包含字母、数字以及下划线,且至少包含2种
- BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~
- centos 7 下vnc弹出窗口太小解决方法
- CentOS6 CentOS7 yum安装图形界面
- bzoj2190 [SDOI2008]仪仗队 - 筛法 - 欧拉函数