2018-05-31 自定义
#import "LoadData.h"
static LoadData *ld;
@implementation LoadData
{
__block NSMutableDictionary *dic;
}
//单利方法
+(instancetype)shareLoadData{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[LoadData alloc]init];
});
return ld;
}
//单利方法
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if(!ld){
ld = [super allocWithZone:zone];
}
return ld;
}
//单利方法
-(id)copy{
return self;
}
//单利方法
-(id)mutableCopy{
return self;
}
//获取内容
-(void)getMessage:(NSString *)url{
//初始化
NSURLSession *session = [NSURLSession sharedSession];
//解析
NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//解析内容
self->dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//接受注册通知
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]postNotificationName:@"dictionay" object:self->dic];
});
}];
//开始请求
[task resume];
}
//注册通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getMessage:) name:@"dictionay" object:nil];
//调用方法
LoadData *ld = [LoadData shareLoadData];
//网址转换字符串
NSString *urlstring = @"https://way.jd.com/jisuapi/get?channel=头条&num=10&start=0&appkey=54e619938bc38b40151c7bc35a29067e";
//字符串转换成UTF8
NSString *urlstringUTF = [urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//获取网址
[ld getMessage:urlstringUTF];
tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tbv.delegate = self;
tbv.dataSource = self;
//注册自定义表格
[tbv registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
//下啦刷新
MJRefreshNormalHeader *header = [MJRefreshNormalHeaderheaderWithRefreshingTarget:self refreshingAction:@selector(loadData)];
//实现刷新
[header setTitle:@"正在刷新。。" forState:MJRefreshStateIdle];
tbv.mj_header = header;
//上拉加载
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
//加载完成
[footer setTitle:@"加载完毕。。。" forState:MJRefreshStateNoMoreData];
tbv.mj_footer = footer;
//添加到主视图
[self.view addSubview:tbv];
-(void)loadData{
// 创建菊花加载控件
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// 回到主线程刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
// 刷新表格
[self->tbvreloadData];
// 刷新时间
[HUDsetMinShowTime:3];
// 一秒后隐藏
[HUDhide:YES];
});
[tbv.mj_header endRefreshing];
}
-(void)loadMore{
[tbv.mj_footer endRefreshing];
}
//内容复制给字典 刷新表格
-(void)getMessage:(NSNotification *)notifi{
dicV = notifi.object;
[tbv reloadData];
}
//几行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//每行几组
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
arrV = dicV[@"result"][@"result"][@"list"];
return arrV.count;
}
//每组的具体内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *str =@"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if(!cell){
//创建cell
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
//表格行高
tbv.rowHeight = 100;
NSDictionary *dic = arrV[indexPath.row];
cell.lab.numberOfLines = 0;
cell.lab.text = dic[@"title"];
cell.lab1.text = dic[@"time"];
NSString *pathURL = dic[@"pic"];
URL = [NSURL URLWithString:pathURL];
[cell.img sd_setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"shareImg.jpg"]];
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:cell.img];
//显示hud的模式
hud.mode = MBProgressHUDAnimationFade;
//背景颜色
hud.color = [UIColor grayColor];
//主标题
hud.labelText = @"正在加载";
//副标题
// hud.detailsLabelText = @"副标题";
//显示、隐藏时的动画样式
hud.animationType = MBProgressHUDAnimationZoomIn;
//当mode的属性是跟进度相关时,就可以设置progress的值,实现实时进度的显示
hud.progress = 0.8;
// HUD的相对于父视图 x 的偏移,默认居中
// hud.xOffset = 50;
// hud.yOffset = 50;
//是否显示蒙板
hud.dimBackground = YES;
//HUD内部视图相对于HUD的内边距
hud.margin = 50;
//HUD的圆角半径
// hud.cornerRadius = 20;
//最小的显示时间
hud.minShowTime = 3.0;
// HUD的最小尺寸
hud.minSize = CGSizeMake(300, 300);
// 代理中只有一个方法,即获得HUD隐藏后的时刻
// hud.delegate = self;
// 加入到img当中
[cell.img addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
//hud执行期间
// NSLog(@"执行期间");
} onQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{
//hud执行完毕
// NSLog(@"执行完毕");
}];
return cell;
}