第一步:依次導(dǎo)入MBProgressHUD、MJRefresh斥滤、SDWebImage播玖、YYModel庫(kù)
第二步:AppDelegate.m
導(dǎo)入頭文件 ViewController.m
ViewController * vc = [[ViewController alloc]init];
UINavigationController * vcNav = [[UINavigationController alloc]initWithRootViewController:vc];
第三步:創(chuàng)建Mymessage繼承NSObject
Mymessage.h中
// 第四層
@interface Mylist : NSObject
@property (nonatomic,strong) NSString * title ;
@property (nonatomic,strong) NSString * time ;
@property (nonatomic,strong) NSString * pic ;
@end
// 第三層
@interfacemyresult :NSObject
@property (nonatomic,strong) NSString * channel ;
@property (nonatomic,strong) NSArray * list ;
@end
// 第二層
@interfaceMYresult :NSObject
@property (nonatomic,strong) myresult * wresult ;
@end
// 第一層
@interfaceMymessage :NSObject
@property (nonatomic,strong) MYresult * qresult ;
@end
Mymessage.m中
@implementation Mylist
@end
// 第三層
@implementation myresult
// 返回容器類(lèi)中的所需要存放的數(shù)據(jù)類(lèi)型 (以 Class 或 Class Name 的形式)肉津。
+ (NSDictionary*)modelContainerPropertyGenericClass {
? ? return@{@"list": [Mylistclass]
?? ? ? ? ? ? };
}
@end
// 第二層
@implementation MYresult
//返回一個(gè) Dict,將 Model 屬性名對(duì)映射到 JSON 的 Key迅箩。
+ (NSDictionary*)modelCustomPropertyMapper {
? ? return @{@"wresult" : @"result",
?? ? ? ? ? ? };
}
@end
// 第一層
@implementation Mymessage
//返回一個(gè) Dict,將 Model 屬性名對(duì)映射到 JSON 的 Key处铛。
+ (NSDictionary*)modelCustomPropertyMapper {
? ? return @{@"qresult" : @"result",
?? ? ? ? ? ? };
}
@end
第四步:ViewController.h中更改UITableViewController
第五步:在ViewController.m中導(dǎo)入頭文件
#import "YYModel.h"
#import "Mymessage.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
輸入鏈接
// 鏈接
#define URL @"https://way.jd.com/jisuapi/get?channel=頭條&num=10&start=5&appkey=54e619938bc38b40151c7bc35a29067e"
簽協(xié)議? MJRefreshBaseViewDelegate
{
? ? //初始化類(lèi)
? ? Mymessage*? message;
? ? // 將解析的數(shù)據(jù)存儲(chǔ)到數(shù)組中
?? __blockNSMutableArray* arr1 ,*arr2 ,*arr3;
? ? // 上下拉刷新
?? ? MJRefreshFooterView *foot;
? ? MJRefreshHeaderView * header ;
? ? // 用來(lái)展示表格cell的個(gè)數(shù)
? ? inti ;
? ? // cell 中添加的圖片框
? ? UIImageView* img ;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? i=5;
? ? // 刷新數(shù)據(jù)
? ? [selfshuaxin];
}
// 刷新顯示數(shù)據(jù)
-(void)shuaxin
{
? ? // 尾部刷新
? ? foot = [MJRefreshFooterView footer];
? ? foot.scrollView = self.tableView;
? ? foot.delegate = self;
? ? // 頭部刷新
? ? header = [MJRefreshHeaderView header];
? ? header.scrollView = self.tableView;
? ? header.delegate = self;
? ? // 開(kāi)始下拉刷新? 直接進(jìn)入回調(diào)方法
? ? [header beginRefreshing];
}
// 刷新進(jìn)入方法
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView*)refreshView{
? ? if([refreshViewisKindOfClass:[MJRefreshHeaderViewclass]]) {
? ? ? ? //2秒以后調(diào)用
? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? ? ? // 10? ? 1
? ? ? ? ? ? // 請(qǐng)求數(shù)據(jù)
? ? ? ? ? ? [selfrequest];
? ? ? ? ? ? [self.tableViewreloadData];
? ? ? ? ? ? // 結(jié)束
? ? ? ? ? ? [headerendRefreshing];
? ? ? ? });
? ? }else{
? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? ? ? // 上拉刷新? 會(huì)出現(xiàn)第二頁(yè)的數(shù)據(jù)
? ? ? ? ? ? i=arr1.count;
? ? ? ? ? ? [self.tableViewreloadData];
? ? ? ? ? ? [footendRefreshing];
? ? ? ? });
? ? }
}
-(void)request
{
? ? // 轉(zhuǎn)換編碼格式
? ? NSString * str = [URL stringByAddingPercentEscapesUsingEncoding:
? ? ? ? ? ? ? ? ? ? ? NSUTF8StringEncoding];
? ? // 請(qǐng)求數(shù)據(jù)
? ? NSURLSession * session = [NSURLSession sharedSession];
? ? NSURLSessionTask* task? = [sessiondataTaskWithURL:[NSURLURLWithString:str]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
? ? ? ? arr1= [[NSMutableArrayalloc]init];
? ? ? ? arr2= [[NSMutableArrayalloc]init];
? ? ? ? arr3= [[NSMutableArrayalloc]init];
? ? ? ? // yymodel? 解析
? ? ? ? message= [Mymessageyy_modelWithJSON:data];
? ? ? ? // 遍歷數(shù)據(jù)
? ? ? ? for (Mylist* list? in message.qresult.wresult.list) {
? ? ? ? ? ? // 加入到數(shù)組中
? ? ? ? ? ? [arr1addObject:list.title];
? ? ? ? ? ? [arr2addObject:list.time];
? ? ? ? ? ? [arr3addObject:list.pic];
? ? ? ? }
? ? ? ? // 回到主線程刷新表格
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [self.tableViewreloadData];
? ? ? ? });
? ? }];
? ? // 開(kāi)始請(qǐng)求
? ? [taskresume];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
#warning Incomplete implementation, return the number of sections
? ? return 1;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
? ? return i;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
? ? // 解決cell 重用 標(biāo)識(shí)符隨 indexPath.row 變化
?? ? NSString* str = [NSStringstringWithFormat:@"cell %ld",indexPath.row];
? ? UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
? ? if(!cell)
? ? {
? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
? ? }
? ? // 添加文字
? ? UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 160, 150)];
? ? lab.text=arr1[indexPath.row];
? ? lab.numberOfLines = 0 ;
? ? [cell.contentViewaddSubview:lab];
? ? // 添加時(shí)間
? ? UILabel * lab2 = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 160, 130, 150, 20)];
? ? lab2.text=arr2[indexPath.row];
? ? lab2.numberOfLines=0;
? ? [cell.contentViewaddSubview:lab2];
? ? // 添加圖片
? ? img = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 160, 0, 150, 130)];
//? ? img.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",arr3[indexPath.row]]]]];
? ? //? sdwebimage? 庫(kù)中的方法用來(lái)請(qǐng)求 網(wǎng)絡(luò)圖片
? ? // placeholderImage 這個(gè)參數(shù)是用來(lái)設(shè)置 還沒(méi)有請(qǐng)求到圖片之前展示的圖片信息
? ? // 簡(jiǎn)單來(lái)說(shuō)就是 一個(gè)占位符
? ? [imgsetImageWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"%@",arr3[indexPath.row]]]placeholderImage:nilcompleted:^(UIImage*image,NSError*error,SDImageCacheTypecacheType) {
? ? ? ? ? ? img.image= image ;
? ? }];
? ? // 調(diào)用加載的進(jìn)度方法
? ? [selfHUD] ;
? ? [cell.contentViewaddSubview:img];
? ? returncell ;
}
// 正在加載的進(jìn)度
-(void)HUD
{
? ? MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:img];
? ? //顯示hud的模式
? ? hud.mode = MBProgressHUDAnimationFade;
? ? //背景顏色
? ? hud.color= [UIColorgrayColor];
? ? //主標(biāo)題
? ? hud.labelText=@"正在加載";
? ? //副標(biāo)題
? ? //? ? hud.detailsLabelText = @"副標(biāo)題";
? ? //顯示饲趋、隱藏時(shí)的動(dòng)畫(huà)樣式
? ? hud.animationType = MBProgressHUDAnimationZoomIn;
? ? //當(dāng)mode的屬性是跟進(jìn)度相關(guān)時(shí)拐揭,就可以設(shè)置progress的值,實(shí)現(xiàn)實(shí)時(shí)進(jìn)度的顯示
? ? hud.progress=0.8;
? ? // HUD的相對(duì)于父視圖 x 的偏移奕塑,默認(rèn)居中
? ? //? ? hud.xOffset = 50;
? ? //? ? hud.yOffset = 50;
? ? //是否顯示蒙板
? ? hud.dimBackground = YES;
? ? //HUD內(nèi)部視圖相對(duì)于HUD的內(nèi)邊距
? ? hud.margin=50;
? ? //HUD的圓角半徑
? ? //? ? hud.cornerRadius = 20;
? ? //最小的顯示時(shí)間
? ? hud.minShowTime=3.0;
? ? // HUD的最小尺寸
? ? hud.minSize=CGSizeMake(300,300);
? ? // 代理中只有一個(gè)方法堂污,即獲得HUD隱藏后的時(shí)刻
? ? //? ? hud.delegate = self;
? ? // 加入到img當(dāng)中
? ? [imgaddSubview:hud];
? ? [hudshowAnimated:YES whileExecutingBlock:^{
? ? ? ? //hud執(zhí)行期間
//? ? ? ? NSLog(@"執(zhí)行期間");
? ? }onQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{
? ? ? ? //hud執(zhí)行完畢
//? ? ? ? NSLog(@"執(zhí)行完畢");
? ? }];
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
? ? return150;
}