#import "ViewController.h"
#import "MJRefresh/MJRefresh.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
? ? //n表格數(shù)組
? ? NSMutableArray * tableArray;
? ? //頁碼
? ? int _curPage;
? ? //時間
? ? NSString * curTime;
? ? //下拉刷新
? ? MJRefreshHeaderView * mjHeadVC;
? ? //上啦刷新
? ? MJRefreshFooterView * mjFooterVC;
}
@property (nonatomic ,strong)UITableView *tbv;
@end
@implementation ViewController
-(UITableView *)tbv{
? ? if (!_tbv) {
? ? ? ? _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
? ? ? ? _tbv.delegate = self;
? ? ? ? _tbv.dataSource = self;
? ? }
? ? return _tbv;
}
#pragma mark -------- UITableDelegate------
#pragma mark -------- UITableDasource-------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
? ? return tableArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"2"];
? ? if (!cell) {
? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"2"];
? ? }
? ? //得到每一個cenll對應的字典
? ? NSDictionary * cellDIC =tableArray[indexPath.row];
? ? cell.textLabel.text=cellDIC[@"content"];
? ? cell.textLabel.numberOfLines = 0;
? ? return cell;
}
-(void)viewWillAppear:(BOOL)animated{
? ? [super viewWillAppear:animated];
? ? //請求數(shù)據(jù)
? ? [self requestDataPost:_curPage time:curTime];
}
#pragma mark? ------------請求網(wǎng)絡(luò)數(shù)據(jù)---------
//get方式請求數(shù)據(jù)
-(void)requestDataGet:(int)page time:(NSString*)time{
}
//post方法
-(void)requestDataPost:(int)page time:(NSString*)time{
? ? //顯示狀態(tài)欄中的z等待想時期
? ? [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
? ? NSURL * url = [NSURL URLWithString:@"http://v.juhe.cn/joke/content/list.php"];
? ? NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0];
? ? [req setHTTPMethod:@"POST"];
? ? NSString * pargerStr = [NSString stringWithFormat:@"key=%@&page=%d&pagesize=10&sort=desc&time=%@",@"eaaf69cdca2f46e403a264f5ef7cb74b",_curPage,curTime];
? ? [req setHTTPBody:[pargerStr dataUsingEncoding:NSUTF8StringEncoding]];
? ? [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
? ? ? ? //回到主線程
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? //停止狀態(tài)欄中的等待指示控件
? ? ? ? ? ? [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
? ? ? ? ? ? //y停止下拉刷新控件
? ? ? ? ? ? [self->mjHeadVC endRefreshing];
? ? ? ? ? //停止上啦刷新控件
? ? ? ? ? ? [self->mjFooterVC endRefreshing];
? ? ? ? });
? ? ? ? if (error !=nil) {
? ? ? ? ? ? NSLog(@"服務(wù)器錯誤");
? ? ? ? ? ? return ;
? ? ? ? }
? ? ? ? NSError * jsonerror = nil;
? ? ? ? id jsData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];
? ? ? ? if (jsonerror !=nil) {
? ? ? ? ? ? NSLog(@"網(wǎng)絡(luò)錯誤");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if ([jsData[@"error_code"]integerValue] !=0) {
? ? ? ? ? ? NSLog(@"%@",jsData[@"reason"]);
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? NSArray * dataARR = [jsData[@"result"]objectForKey:@"data"];
? ? ? ? if (self->_curPage==1) {
? ? ? ? ? ? ? self->tableArray = [dataARR mutableCopy];
? ? ? ? }else{
? ? ? ? ? ? [self->tableArray addObjectsFromArray:dataARR];
? ? ? ? }
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [self.tbv reloadData];
? ? ? ? });
? ? }]resume];
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ? [self.view addSubview:self.tbv];
? ? //給當前頁碼默認值
? ? _curPage = 1;
? ? //給時間字符串賦值
? ? NSDate * now = [NSDate date];
? ? NSTimeInterval interval =[now timeIntervalSince1970];
? ? curTime = [NSString stringWithFormat:@"%d",(int)interval];
? ? //將下拉刷新實現(xiàn)
? ? mjHeadVC = [[MJRefreshHeaderView alloc]initWithScrollView:self.tbv];
? ? //使用week解決循環(huán)強引用
? ? __weak typeof (self) weakSelf = self;
? ? //定義下拉回調(diào)的代碼塊
? ? mjHeadVC.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
? ? ? ? //用一個強引用對weak修飾? 防止內(nèi)存釋放
? ? ? ? __strong typeof (self) strongSelf = weakSelf;
? ? ? ? //重新初始化頁碼和當前時間
? ? ? ? self->_curPage=1;
? ? ? ? NSTimeInterval interval =[now timeIntervalSince1970];
? ? ? ? self->curTime = [NSString stringWithFormat:@"%d",(int)interval];
? ? ? ? //重新獲取網(wǎng)絡(luò)數(shù)據(jù)
? ? ? ? [strongSelf requestDataPost:strongSelf->_curPage time:strongSelf->curTime];
? ? };
? ? //上啦刷新l控件
? ? mjFooterVC = [[MJRefreshFooterView alloc]initWithScrollView:self.tbv];
? ? //設(shè)置回調(diào)代碼塊
? ? mjFooterVC.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
? ? ? ? //用一個強引用對weak修飾? 防止內(nèi)存釋放
? ? ? ? __strong typeof (self) strongSelf = weakSelf;
? ? ? ? //改變頁碼
? ? ? ? self->_curPage+=1;
? ? ? ? //重新獲取網(wǎng)絡(luò)數(shù)據(jù)
? ? ? ? [strongSelf requestDataPost:strongSelf->_curPage time:strongSelf->curTime];
? ? };
}
@end