MBProgressHUD+MJRefresh+SDWebimage三合一

//第一步:依次导入MBProgressHUD、MJRefresh、SDWebimage三个库

//第二步:在AppDelgate.m中添加头文件#import "ViewController.h"

//在此方法中添加以下代码:- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

   self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

//第三步:修改ViewController.h中的基类 将UIViewController 修改为 UITableViewController

//第四步:在ViewController.m 依次添加以下头文件

//导入HUD头文件

#import "MBProgressHUD.h"

//导入Image头文件

#import "UIImageView+WebCache.h"

//导入刷新头文件

#import "MJRefresh.h"

//第五步:声明二个属性

@interface ViewController ()

{

    NSURL* imageUrl;

    UIImageView*image;

}

@end


//添加以下代码

- (void)viewDidLoad {

    [super viewDidLoad];

//设置标题

self.title = @"SDWebImage";

//设置图片位置

    image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

 [self.view addSubview:image];

//创建图片网址:

//注意事项:因图片网址是http开头的,需要在info.plist添加以下网络授权

/*

NSAppTransportSecurity

NSAllowsArbitraryLoads

*/

 imageUrl = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/b7fd5266d0160924842b3f21d30735fae6cd34f9.jpg"];

// 在根视图显示,在根视图内部滚动

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    // 触发异步任务,给UIKit机会重绘智慧HUD添加到视图层次结构。

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{

  [self   doSomeWork];

 dispatch_async(dispatch_get_main_queue(), ^{

            [hud  hideAnimated:YES];

        });

    });

}

#pragma mark -

#pragma mark doSomeWork

- (void)doSomeWork {

//设置睡眠时间

 sleep(3.0);

    //  placeholderImage 是占位图 如果图片没下载下来之前  会使用placeholderImage占位图的图片来显示

    [image sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"strawberry"]];

 [self tableViewMJRefresh];

}

// 刷新

- (void)tableViewMJRefresh

{

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        // 进入刷新状态后会自动调用这个block

        NSLog(@"继续加载图片");

    }];

    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];

}

// 加载数据

- (void)loadNewData

{

  [self.tableView.header endRefreshing];

//刷新替换新图片,如不需要更换新图片,以下二行代码不需要添加

    imageUrl = [NSURL URLWithString:@"http://s1.lashouimg.com/zt/201110/27/131972321636938600.jpg"];

        [self   doSomeWork];

}

//cell的个数

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

    return 1;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@""];

  if(!cell) {

        cell  =  [[UITableViewCellalloc]init];

    }

//隐藏表格分割线

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


    returncell;

}

你可能感兴趣的:(MBProgressHUD+MJRefresh+SDWebimage三合一)