你的位置:首页 > 操作系统

[操作系统]UI_异步下载


#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
{
    NSURLConnection *_connection;
    NSMutableData *_data;
    NSMutableArray *_dataList;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor=[UIColor cyanColor];
    // Do any additional setup after loading the view, typically from a nib.
    _data=[NSMutableData data];
    _dataList =[NSMutableArray array];
    [self loadDataWithPage:1];
   
}
-(void)loadDataWithPage:(NSInteger)pageIndex
{
     //1.创建NSURL对象
    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited//?currency=rmb&page=%li",pageIndex]];
    //2.创建网络请求
    NSURLRequest *request =[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
    //3.创建NSURLConnection,通过代理下载数据,遵守<NSURLConnectionDataDelegate>代理
    _connection =[NSURLConnection connectionWithRequest:request delegate:self];
    
}

#pragma mark   ----NSURLConnectionDataDelegate协议方法-------
//1.接受完HTTP协议头,开始真正接受数据时调用,一般在这个方法里初始化一些存储数据的对象,如:NSMutableData
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse  *httpResponse =(NSHTTPURLResponse *)response;
    //
   NSLog(@"%@",httpResponse.allHeaderFields);
    //打印状态码,200请求成功。404表示请求数据失败,403表示服务器错误
    //NSLog(@"%li",httpResponse.statusCode);
    //清空数据,准备接受新的数据
    [_data setLength:0];
}
//接收到数据后,调用此方法,此方法可能被调用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //追加数据
    [_data appendData:data];
}

//数据下载完成时被调用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //解析数据
    if (_data) {
        id Json =[NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
        if ([Json isKindOfClass:[NSDictionary class]]) {
            [_dataList addObjectsFromArray:Json[@"applications"]];//copy,也可以
        }
        NSLog(@"%@",_dataList);
        //重新加载tableView
        [self.tableView reloadData];
    }
}
//数据请求失败时调用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //打印错误信息
    NSLog(@"%@",error.localizedDescription);
}
#pragma mark ------UITableViewDelegateSourse----

-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio
{
    return _dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID =@"cellID";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.textLabel.text=[_dataList[indexPath.row]objectForKey:@"name"];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"下载次数:%@",_dataList[indexPath.row][@"downloads"]];
    //iconUrl = "http://photo.candou.com/i/114/8c1b645b8d110973b14b9934315ce970";
    NSURL  *iconUrl =_dataList[indexPath.row][@"iconUrl"];
    [cell.imageView setImageWithURL:iconUrl placeholderImage:[UIImage imageNamed:@"lqcq.jpg"] ];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}