UI第十七讲.图片异步加载(包括第三方), KVO, KVC

 一、异步下载图片二、UITableView中图片的异步下载

示例代码:

图片解析,并利用第三方方法对图片进行异步加载

#import "ViewController.h"
#import "TableViewCell.h"
#import "NetWorkHandle.h"
#import "Model.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

//@property(nonatomic,strong)UIImageView *imagev;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self loadTableView];
    [self loadPOSTImage];
 
    
}

-(void)loadTableView
{

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
   
    //注意这里要写自己定义的tableViewCell
     [self.tableView registerClass:[TableViewCell class]forCellReuseIdentifier:@"CELL"];
    

}

//自定义高度********************
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;

}

//对图片到的的解析,和利用第三方进行异步加载
-(void)loadPOSTImage
{
    self.dataArray  = [NSMutableArray array];
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
    //用封装对的GET/POST异步进行加载
    [NetWorkHandle getDataWithURLString:str compare:^(id object) {
        //用字典接收请求下来的数据
        NSDictionary *dic = object;
        //用数组取出字典中的数据
        NSArray *arr = dic[@"events"];
        
        //利用for/in进行遍历
        for (NSDictionary *dict in arr) {
            //先初始化
            Model *model = [[Model alloc] init];
            //然后用model取出字典中的数据
            [model setValuesForKeysWithDictionary:dict];
            //将model中数据添加进数组
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
    }];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    
    Model *model = self.dataArray[indexPath.row];
    

    //SDWebImage 功能:1.缓存,1.异步加载
    //使用第三方进行图片的设置
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
      cell.label.text = model.title;
  
    return cell;


}

三、KVO

四、KVO监测Model图片下载

原文地址:https://www.cnblogs.com/erdeng/p/4865390.html