SDWebImage的用法

20 篇文章 0 订阅
19 篇文章 0 订阅


#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 {

    [super viewDidLoad];


    // 注册cell

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

    

    

    [self.view addSubview:self.OneView];

    [self.view addSubview:self.TwoTableView];

    

    self.TwoTableView.delegate = self;

    self.TwoTableView.dataSource = self;

    [self loadData];

}

#pragma mark 加载数据

- (void)loadData {

    self.AllDataArray = [NSMutableArray array];

    __block typeof(self) weakSelf = self;

    // 准备网址

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

    // 创建请求对象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 设置请求类型

    [request setHTTPMethod:@"GET"];

    // 连接服务器

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        

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

        if (data == nil) {

            return ;

        }

        // 解析

        

        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        

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

        for (NSDictionary *item in array) {

            one *l = [one new];

            [l setValuesForKeysWithDictionary:item];

            

            

            [weakSelf.AllDataArray addObject:l];

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

            

        }

        [self.TwoTableView reloadData]; // 刷新数据

        

    }];

    

    

    

    


}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark 懒加载

- (UIView *)OneView {

    if (_OneView == nil) {

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

        _OneView.backgroundColor = [UIColor redColor];

    }

    return _OneView;

}


- (UITableView *)TwoTableView {

    if (_TwoTableView == nil) {

        _TwoTableView = [[UITableView alloc] 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 = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"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.imageView sd_setImageWithURL:[NSURL URLWithString:l.mediumLogo] placeholderImage:[UIImage imageNamed:@"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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值