上拉加载
和前边类似,只是view放的位置不同,计算的方式不一样而已,原理是相同的
贴个图,这个就可以看清楚了
就直接贴代码了
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong, nonatomic)UITableView * tableView;
@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];
[self createRefreshView];
[self createTableView];
[self createData];
_dataArray = [[NSMutableArray alloc]init];
}
-(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];
}
-(void)createTableView
{
_tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
-(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) {
_refreshLabel.text = @"上拉加载";
[_tableView reloadData];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
-(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;
}
-(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;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"contentOffset : %@",NSStringFromCGPoint(scrollView.contentOffset));
NSLog(@"contentSize : %@",NSStringFromCGSize(scrollView.contentSize));
NSLog(@"frame : %@",NSStringFromCGRect(scrollView.frame));
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];
}];
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
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];
}
@end