SDWebImage+MJRefresh+MBBrogressHUD的應(yīng)用

第一步:依次導(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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市龄砰,隨后出現(xiàn)的幾起案子盟猖,更是在濱河造成了極大的恐慌,老刑警劉巖换棚,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件式镐,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡圃泡,警方通過(guò)查閱死者的電腦和手機(jī)碟案,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)颇蜡,“玉大人价说,你說(shuō)我怎么就攤上這事》绯樱” “怎么了鳖目?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)缤弦。 經(jīng)常有香客問(wèn)我领迈,道長(zhǎng),這世上最難降的妖魔是什么碍沐? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任狸捅,我火速辦了婚禮,結(jié)果婚禮上累提,老公的妹妹穿的比我還像新娘尘喝。我一直安慰自己,他們只是感情好斋陪,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布朽褪。 她就那樣靜靜地躺著,像睡著了一般无虚。 火紅的嫁衣襯著肌膚如雪缔赠。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,682評(píng)論 1 312
  • 那天友题,我揣著相機(jī)與錄音嗤堰,去河邊找鬼。 笑死咆爽,一個(gè)胖子當(dāng)著我的面吹牛梁棠,可吹牛的內(nèi)容都是我干的置森。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼符糊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼凫海!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起男娄,我...
    開(kāi)封第一講書(shū)人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤行贪,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后模闲,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體建瘫,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年尸折,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了啰脚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡实夹,死狀恐怖橄浓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亮航,我是刑警寧澤荸实,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站缴淋,受9級(jí)特大地震影響准给,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜重抖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一露氮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钟沛,春花似錦沦辙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)详民。三九已至延欠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間沈跨,已是汗流浹背由捎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留饿凛,地道東北人狞玛。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓软驰,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親心肪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子锭亏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容