MJRefresh刷新Demo

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 960评论 0 1
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,452评论 9 5
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,538评论 1 14
  • 七月的阳光很温暖也很明亮,阳光从街道边上的树叶中间打下一片斑驳的影子。翠绿的叶子在阳光里摇晃,如同一个调皮的孩童。...
    艾小咩阅读 1,016评论 28 16