SDWebImage的用法

来源:互联网 发布:淘宝上评论在哪里 编辑:程序博客网 时间:2024/05/17 09:33


#import "ViewController.h"

#import “one.h” //创建的模型、里面声明属性

#import "UIImageView+WebCache.h"

//#import "firstTableViewCell.h" // 使用xib的话引入这个创建的头文件


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,strong) UITableView *TwoTableView;

@property (nonatomic,strong) UIView *OneView;

@property (nonatomic,strong) NSMutableArray *AllDataArray;


@end



@implementation ViewController



- (void)viewDidLoad {

    [superviewDidLoad];


    // 注册cell

   // [self.TwoTableView registerNib:[UINib nibWithNibName:@"firstTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"]; //使用自定义的cell要注册

    

    

    [self.viewaddSubview:self.OneView];

    [self.viewaddSubview:self.TwoTableView];

    

    self.TwoTableView.delegate =self;

    self.TwoTableView.dataSource =self;

    [selfloadData];

}

#pragma mark 加载数据

- (void)loadData {

    self.AllDataArray = [NSMutableArrayarray];

    __blocktypeof(self) weakSelf =self;

    // 准备网址

    NSURL *url = [NSURLURLWithString:@"http://app.9nali.com/index/778?page_id=1&device=iPhone&version=1.1.2"];

    // 创建请求对象

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

    // 设置请求类型

    [request setHTTPMethod:@"GET"];

    // 连接服务器

    [NSURLConnectionsendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {

        

        // 判断数据是否为空,如果是的话就直接返回

        if (data ==nil) {

            return ;

        }

        // 解析

        

        NSMutableDictionary *dict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];

        

        NSArray *array = dict[@"list"];

        for (NSDictionary *itemin array) {

            one *l = [onenew];

            [l setValuesForKeysWithDictionary:item];

            

            

            [weakSelf.AllDataArrayaddObject:l];

            NSLog(@"%@", weakSelf.AllDataArray[0]);

            

        }

        [self.TwoTableViewreloadData]; //刷新数据

        

    }];

    

    

    

    


}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark 懒加载

- (UIView *)OneView {

    if (_OneView ==nil) {

        _OneView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 375,200)];

        _OneView.backgroundColor = [UIColorredColor];

    }

    return_OneView;

}


- (UITableView *)TwoTableView {

    if (_TwoTableView ==nil) {

        _TwoTableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,200, 375,300) style:UITableViewStyleGrouped];

    }

    return_TwoTableView;

}



#pragma mark 实现tabllView的代理方法

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

    return_AllDataArray.count;

}


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

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];

    

    if (!cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];

    }

    one *l =_AllDataArray[indexPath.row];

    cell.textLabel.text = l.nickname;

//    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:l.mediumLogo] placeholderImage:[UIImage imageNamed:@"music.jpg"]];

    

    

    [cell.imageViewsd_setImageWithURL:[NSURLURLWithString:l.mediumLogo]placeholderImage:[UIImageimageNamed:@"music.jpg"]completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType,NSURL *imageURL) {

        

    }];

    return cell;

}

/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


0 0