iOS开发 ----- 上拉加载

上拉加载

和前边类似,只是view放的位置不同,计算的方式不一样而已,原理是相同的

源代码在这里

这里是下拉刷新

贴个图,这个就可以看清楚了

这里写图片描述

就直接贴代码了

//
//  ViewController.m
//  上拉加载
//
//  Created by xiaoma on 15/9/29.
//  Copyright (c) 2015年 xiaoma. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

//tableView
@property(strong, nonatomic)UITableView * tableView;
//刷新出来的View的总view
@property(strong, nonatomic)UIView * refreshView;
//上边的字体
@property(strong, nonatomic)UILabel * refreshLabel;
//当前页
@property(assign, nonatomic)NSInteger page;
//存放数据的数组
@property(strong, nonatomic)NSMutableArray * dataArray;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createRefreshView];
    [self createTableView];
    [self createData];
    _dataArray = [[NSMutableArray alloc]init];
}


//创建刷新的view,在屏幕外边,先添加到屏幕上,然后在添加tableView
-(void)createRefreshView
{
    _refreshView = [[UIView alloc]initWithFrame:CGRectMake(0, 657, 375, 100)];
    _refreshView.backgroundColor = [UIColor whiteColor];
    _refreshLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 100)];
    _refreshLabel.text = @"上拉加载";
    _refreshLabel.font = [UIFont systemFontOfSize:30];
    _refreshLabel.textColor = [UIColor redColor];
    _refreshLabel.textAlignment = NSTextAlignmentCenter;

    [_refreshView addSubview:_refreshLabel];
    [self.view addSubview:_refreshView];
}


//创建tableView
-(void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;

    [self.view addSubview:_tableView];
}


//创建数据,用到AFnetWorking,本来想用NSURLSession的,这个类貌似是由于线程的原因,要手从戳一下屏幕才可以显示数据
//效果并不好,所以用了AFNetWorking
//下边是解析数据,没什么好说的,
-(void)createData
{

    NSString * path = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%ld",_page];


    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        //继续添加数据
        NSArray * array = [responseObject objectForKey:@"applications"];

        for (NSDictionary * temp in array) {
            [_dataArray addObject:[temp objectForKey:@"name"]];
        }

        //修改偏移量,这个下边在加载的时候,修改了偏移量,这里改回来
        [UIView animateWithDuration:0.2 animations:^{
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        } completion:^(BOOL finished) {
            //动画结束之后,修改label的文本显示
            //然后重载数据
            _refreshLabel.text = @"上拉加载";
            [_tableView reloadData];

        }];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

}

//tableView的相关代理
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
//设置有多少条数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
//有几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//给cell赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * string = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}


//检测tableView的滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //这里有几个值要说明一下,
    //scrollView.contentOffset 这个时偏移量,下拉的时候这个时负数
    //scrollView.contentSize 这个一般来说,会比当前的最大偏移量多出一个屏幕左右
    //scrollView.frame tableView的frame
    NSLog(@"contentOffset : %@",NSStringFromCGPoint(scrollView.contentOffset));
    NSLog(@"contentSize   : %@",NSStringFromCGSize(scrollView.contentSize));
    NSLog(@"frame         : %@",NSStringFromCGRect(scrollView.frame));

    //让上边的动画,跟着table的位置变化而变化,由于时先添加的,所以到带到前边
    if (scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height) {

        CGFloat move = scrollView.contentOffset.y + scrollView.frame.size.height - scrollView.contentSize.height;
        [UIView animateWithDuration:0.1 animations:^{
            _refreshView.frame = CGRectMake(0, 667- move, 375, 100);
            [self.view bringSubviewToFront:_refreshView];

        }];
    }
}

//tableView滚动结束后调用的方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //当检测到偏移量多余150点的时候,就让tableView上边空出100点来显示刷新view
    //然后显示正在刷新
    //重载数据
    CGFloat offset = scrollView.contentOffset.y + scrollView.frame.size.height - scrollView.contentSize.height;

    if (offset > 150) {
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
        _refreshLabel.text = @"正在加载数据";

        _page++;
        [self createData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值