当前位置: 首页 > 工具软件 > MJRefresh > 使用案例 >

MJRefresher

濮金鑫
2023-12-01
//
//  ViewController.m
//  MJRefresh
//
//  Created by lanou on 15/10/26.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking.h>
#import <MJRefresh.h>

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *datasource;
@property(nonatomic, strong)NSNumber *page;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor yellowColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    self.page = @0;
    [self addMJRefreshWidget];
    [self refreshMainPage];
}
-(void)addMJRefreshWidget
{
    //add MJrefresh Hesder and Footer
    //create custom headerRefresh images
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0 ; i < 8 ; i++) {
        NSString *string = [NSString stringWithFormat:@"refresh%d",i+1];
        UIImage *image = [UIImage imageNamed:string];
        [images addObject:image];
    }
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 150, 150)];
    imageview.animationDuration = 5;
    imageview.animationImages = images;
    imageview.animationRepeatCount = 0 ;
    [imageview startAnimating];
    MJRefreshGifHeader *headerRefresher = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullDownToRefresh)];
    [headerRefresher setImages:images forState:MJRefreshStateIdle];
    [headerRefresher setImages:images forState:MJRefreshStatePulling];
    [headerRefresher setImages:images forState:MJRefreshStateRefreshing];
    self.tableView.header = headerRefresher;
    
    //normal style
//    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//       [self pullDownToRefresh];
//   }];
    // 设置文字(默认的文字在MJRefreshConst中修改)
//    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
//    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
//    self.tableView.headerRefreshingText = @"刷新中。。。";
//    
//    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
//    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
//    self.tableView.footerRefreshingText = @"加载中。。。";


    
    //create footer
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSInteger temp = self.page.integerValue;
        self.page = [NSNumber numberWithInteger:(++temp)];
        [self refreshMainPage];
    }];
}

-(void)refreshMainPage
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *requestURL = @"http://api.qctt.cn/qctt-api/4.9/index.php/News/getNewsList";
    NSDictionary *getNewsListParameter = @{@"page" : self.page, @"classId" : @0};
    [manager POST:requestURL parameters:getNewsListParameter success:^(AFHTTPRequestOperation * operation, id responseObject) {
        //NSLog(@"net request successful%@", responseObject);
        //get the datasource and set values for model
        if ([[responseObject objectForKey:@"statusCode"] isEqualToNumber:@400]) {
            NSArray *dataArray = [[responseObject objectForKey:@"data"] objectForKey:@"newsList"];
            for (int i = 0; i < dataArray.count; i++) {
                NSDictionary *dic = dataArray[i];
                NSString *title = [dic objectForKey:@"title"];
                [self.datasource addObject:title];
            }
            //end refresh before reloadData
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            //reload data in screen
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation * operation, NSError * responseObject) {
        NSLog(@"网络请求错误:%@", responseObject);
        
    }];
}
-(void)pullDownToRefresh
{
    self.page = @0;
    [self refreshMainPage];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.textLabel.text = self.datasource[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - lazy loading
-(UITableView *)tableView
{
    if (!_tableView) {
        CGRect rect = self.view.bounds;
        rect.origin.y = 20;
        rect.size.height = CGRectGetHeight(self.view.bounds) - 20;
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
-(NSMutableArray *)datasource
{
    if (!_datasource) {
        self.datasource = [NSMutableArray array];
    }
    return _datasource;
}

@end

 类似资料:

相关阅读

相关文章

相关问答