//
// ViewController.m
// UI11_网络解析
//
// Created by lanou3g on 17/8/17.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
#import "Song.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *songArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) BOOL isRefresh;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.songArray = [NSMutableArray array];
self.pageNumber = 1;
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
self.tableView = tableView;
NSString *URLString = @"http://172.18.26.196/get.php?page=1";
[self getDataWithURLString:URLString];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;
if (contentHeight <= scrollView.frame.size.height) {
contentHeight = scrollView.frame.size.height;
}
if (offsetY >= contentHeight-scrollView.frame.size.height+50) {
self.isRefresh = NO;
self.pageNumber++;
NSString *URLString = [NSString stringWithFormat:@"http://172.18.26.196/get.php?page=%ld",self.pageNumber];
[self getDataWithURLString:URLString];
} else if (offsetY <= -80.f) {
self.isRefresh = YES;
self.pageNumber = 1;
NSString *URLString = [NSString stringWithFormat:@"http://172.18.26.196/get.php?page=%ld",self.pageNumber];
[self getDataWithURLString:URLString];
}
NSLog(@"%lf",offsetY);
}
- (void)getDataWithURLString:(NSString *)URLString {
NSURL *url = [NSURL URLWithString:URLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//异步 主线程
dispatch_async(dispatch_get_main_queue(), ^{
if (self.isRefresh) {
[self.songArray removeAllObjects];
}
if (!error) {
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result:%@",result);
for (NSDictionary *songDic in result) {
Song *song = [[Song alloc] init];
[song setValuesForKeysWithDictionary:songDic];
[self.songArray addObject:song];
}
//刷新tableView数据
[self.tableView reloadData];
} else {
NSLog(@"error:%@",error);
}
});
}];
[dataTask resume];
}
- (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 *songId = @"song";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:songId];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:songId];
}
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
// UI11_网络解析
//
// Created by lanou3g on 17/8/17.
// 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
// UI11_网络解析
//
// Created by lanou3g on 17/8/17.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "Song.h"
@implementation Song
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end