IOS -- 上拉下拉刷新MJRefresh的使用

IOS -- 上拉下拉刷新MJRefresh的使用_第1张图片
Untitled.gif

旧方法
1.导入头文件#import "MJRefresh/MJRefresh.h"

//

#import "ViewController.h"
#import "MJRefresh/MJRefresh.h"
@interface ViewController (){
    UITableView *table;
    NSMutableArray *arr;
    MJRefreshFooterView *foot;
    MJRefreshHeaderView *header;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview: table];
    arr = [NSMutableArray arrayWithObjects:@"123",@"345",@"123",@"345",@"123",@"345", nil];
    
    
    foot = [MJRefreshFooterView footer];
    foot.scrollView = table;
    foot.delegate = self;
    
    
    header = [MJRefreshHeaderView header];
    header.scrollView = table;
    header.delegate = self;
    
    [header beginRefreshing];
    
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{
    if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {
        //2秒以后调用
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 10     1
            [arr addObject:@"new123"];
            
            [table reloadData];
            [header endRefreshing];
        });
    }else{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [arr removeObjectAtIndex:0];
            [table reloadData];
            [foot endRefreshing];
        });
    }
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellId = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = arr[indexPath.row];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}


@end

新方法cocoapods导入

#import "ViewController.h"
#import 
@interface ViewController (){
    UITableView *table;
    NSMutableArray *tableArr;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    tableArr = [NSMutableArray arrayWithObjects:@"1", @"2", @"tata" ,nil];
    table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [table.mj_header endRefreshing];
        [tableArr addObject:@"666"];
        [table reloadData];
    }];
    table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [table.mj_footer endRefreshing];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"tableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.textLabel.text = tableArr[indexPath.row];
    return cell;
}

你可能感兴趣的:(IOS -- 上拉下拉刷新MJRefresh的使用)