寫刷新加載,我用到的是第三方框架门扇,首先您可以到github下載MJRefresh框架,接下來(lái)看代碼。
//
// EAHTTPCliecnt.h
// HTTPClient
//
// Created by Eiwodetianna on 2017/8/16.
// Copyright ? 2017年 lanou. All rights reserved.
//
#import <Foundation/Foundation.h>
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];
// 設(shè)置請(qǐng)求超時(shí)時(shí)間
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;
}
}
// 網(wǎng)絡(luò)請(qǐng)求時(shí)將URL轉(zhuǎn)變?yōu)楹戏ǜ袷? URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:URLString];
request.URL = url;
NSURLSession *session = [NSURLSession sharedSession];
// 加載數(shù)據(jù)任務(wù)
// 參數(shù)1:請(qǐng)求對(duì)象
// 參數(shù)2:請(qǐng)求結(jié)束后回調(diào)函數(shù)
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);
}
});
}];
// 執(zhí)行任務(wù)
[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 () <UITableViewDelegate,UITableViewDataSource>
@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];
//結(jié)束刷新
[self.tableView.mj_header endRefreshing];
}];
//自動(dòng)刷新
[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]};
//網(wǎng)絡(luò)請(qǐng)求
[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 <Foundation/Foundation.h>
@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