写刷新加载,我用到的是第三方框架,首先您可以到github下载MJRefresh框架,接下来看代码。
//
// EAHTTPCliecnt.h
// HTTPClient
//
// Created by Eiwodetianna on 2017/8/16.
// Copyright © 2017年 lanou. All rights reserved.
//
#import
typedef void(^EASuccessBlock)(id reponseObject);
typedef void(^EAFailureBlock)(NSError *error);
@interface EAHTTPClient : NSObject
+ (void)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(EASuccessBlock)success failure:(EAFailureBlock)failure;
+ (void)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(EASuccessBlock)success failure:(EAFailureBlock)failure;
@end
//
// EAHTTPCliecnt.m
// HTTPClient
//
// Created by Eiwodetianna on 2017/8/16.
// Copyright © 2017年 lanou. All rights reserved.
//
#import "EAHTTPClient.h"
typedef NS_ENUM(NSUInteger, EAHTTPMethod) {
EAHTTPMethodGET,
EAHTTPMethodPOST,
};
@interface EAHTTPClient ()
@end
@implementation EAHTTPClient
+ (void)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(EASuccessBlock)success failure:(EAFailureBlock)failure {
[self requestWithURLString:URLString parameters:parameters method:EAHTTPMethodGET success:success failure:failure];
}
+ (void)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(EASuccessBlock)success failure:(EAFailureBlock)failure {
[self requestWithURLString:URLString parameters:parameters method:EAHTTPMethodPOST success:success failure:failure];
}
+ (void)requestWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(EAHTTPMethod)method success:(EASuccessBlock)success failure:(EAFailureBlock)failure {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置请求超时时间
request.timeoutInterval = 60.f;
NSMutableArray *parameterArray = [NSMutableArray array];
for (NSString *key in parameters) {
NSString *parameterString = [NSString stringWithFormat:@"%@=%@", key, parameters[key]];
[parameterArray addObject:parameterString];
}
NSString *query = [parameterArray componentsJoinedByString:@"&"];
switch (method) {
case EAHTTPMethodGET:{
request.HTTPMethod = @"GET";
URLString = [URLString stringByAppendingFormat:@"?%@", query];
break;
}
case EAHTTPMethodPOST: {
request.HTTPMethod = @"POST";
request.HTTPBody = [query dataUsingEncoding:NSUTF8StringEncoding];
break;
}
}
// 网络请求时将URL转变为合法格式
URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:URLString];
request.URL = url;
NSURLSession *session = [NSURLSession sharedSession];
// 加载数据任务
// 参数1:请求对象
// 参数2:请求结束后回调函数
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
if (!error) {
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// id result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
success(result);
} else {
failure(error);
}
});
}];
// 执行任务
[dataTask resume];
}
@end
//
// ViewController.m
// UI12_刷新加载
//
// Created by lanou3g on 17/8/18.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
#import "MJRefresh.h" //主头文件
#import "EAHTTPClient.h"
#import "Song.h"
@interface ViewController ()
@property (nonatomic,retain) NSMutableArray *songArray;
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,assign,getter=isRefresh) BOOL refresh;
@property (nonatomic,assign) NSInteger page;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.songArray = [NSMutableArray array];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tableView];
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
NSLog(@"olivia");
self.refresh = YES;
self.page = 1;
[self loadDataWithPage:self.page];
//结束刷新
[self.tableView.mj_header endRefreshing];
}];
//自动刷新
[self.tableView.mj_header beginRefreshing];
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
NSLog(@"lee");
self.refresh = NO;
// [self.tableView.mj_footer endRefreshing];
self.page++;
[self loadDataWithPage:self.page];
}];
}
- (void)loadDataWithPage:(NSInteger)pageNumber {
NSString *URLString = @"http://172.18.26.201/get.php";
NSDictionary *parameters = @{@"page":[NSString stringWithFormat:@"%ld",pageNumber]};
//网络请求
[EAHTTPClient GET:URLString parameters:parameters success:^(id reponseObject) {
if (self.isRefresh) {
[self.songArray removeAllObjects];
[self.tableView.mj_header endRefreshing];
}else {
[self.tableView.mj_footer endRefreshing];
}
for (NSDictionary *songDic in reponseObject) {
Song *song = [[Song alloc] init];
[song setValuesForKeysWithDictionary:songDic];
[self.songArray addObject:song];
}
[self.tableView reloadData];
} failure:^(NSError *error) {
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.songArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Song *song = self.songArray[indexPath.row];
static NSString *cellId = @"cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = song.name;
cell.detailTextLabel.text = song.singerName;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// Song.h
// UI12_刷新加载
//
// Created by lanou3g on 17/8/18.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import
@interface Song : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *number;
@property (nonatomic, copy) NSString *pic;
@property (nonatomic, copy) NSString *singerName;
@property (nonatomic, copy) NSString *url;
@end
//
// Song.m
// UI12_刷新加载
//
// Created by lanou3g on 17/8/18.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "Song.h"
@implementation Song
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end