//  AppDelegate.m
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor]; return YES;
}
//  ViewController.h
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UITableViewController <NSURLConnectionDataDelegate> @end //
// ViewController.m
// UI2_异步下载
//
// Created by zhangxueming on 15/7/17.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "UIImageView+WebCache.h" //异步下载:向服务器发送请求后,UI主线程能继续交互,另外单独创建一个线程下载数据 @interface ViewController ()
{
NSURLConnection *_urlConntecion;//建立客户端与服务器的连接
NSMutableArray *_dataList; //数据源
NSMutableData *_receiveData; //存储服务器下发的数据
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
_dataList = [NSMutableArray array];
_receiveData = [NSMutableData data];
[self loadDataWithPage:10];
} - (void)loadDataWithPage:(NSInteger)page
{
//1.创建NSURL
NSString *urlString = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%ld", page];
NSURL *url = [NSURL URLWithString:urlString]; //2.创建网络请求对象
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; //3.通过NSURLConnection对象实现异步下载数据
_urlConntecion = [[NSURLConnection alloc] initWithRequest:request delegate:self];
} #pragma mark ---NSURLConnectionDataDelegate--- //当客户端接受到服务器的响应,调用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//打印状态码
NSLog(@"statusCode = %li", ((NSHTTPURLResponse *)response).statusCode);
//清空数据
[_receiveData setLength:0];
} //当从服务器接受到数据的时候, 调用此方法,如果数据量比较大, 该方法被调用多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receiveData appendData:data];
} //当数据完成下载, 调用该方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//数据解析
id result = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingMutableContainers error:nil];
if ([result isKindOfClass:[NSDictionary class]])
{
NSLog(@"result = %@", result);
NSArray *app = [result objectForKey:@"applications"];
[_dataList addObjectsFromArray:app];
}
else if([result isKindOfClass:[NSArray class]])
{ }
//刷新UI
[self.tableView reloadData];
} //下载数据失败的时候调用该方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//打印错误信息
NSLog(@"%@", error.localizedDescription);
} #pragma mark ---UITableViewDataSource--- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataList.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
}
//name
//currentPrice
//iconUrl
NSDictionary *dict = _dataList[indexPath.row];
cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text =[NSString stringWithFormat:@"原价:%@RMB",[dict objectForKey:@"lastPrice"]];
NSString *iconUrl = [dict objectForKey:@"iconUrl"];
//图片异步加载并设置默认图片
[cell.imageView setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"holderImage"]]; return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最新文章

  1. IOS跑马灯效果,实现文字水平无间断滚动
  2. Eclipse Kelper 设置代理服务器无效解决方案
  3. 嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞
  4. Html中模态框(弹出框)使用入门
  5. 3个常用基于Linux系统命令行WEB网站浏览工具(w3m/Links/Lynx)
  6. 黄聪:C# 开发Chrome内核浏览器(WebKit.net)
  7. 172. Factorial Trailing Zeroes
  8. (原)10-folder交叉验证
  9. Flask -- 会话
  10. php工作两年了。。。
  11. MyEclipse Java基础使用笔记
  12. MySQL 数据类型和约束(外键是重点&#128580;)
  13. Selenium和Firefox兼容问题
  14. 我的ElasticSearch之ElasticSearch安装配置环境
  15. 图片转化成base64字符串
  16. 将scrapy项目运行在pycharm中
  17. Git 远程仓库 更新url
  18. IntelliJ IDEA中文乱码问题
  19. opencv之图像拼接
  20. C#中一道关于员工信息系统的题(主要考察LinQ和正则表达式验证)

热门文章

  1. 1123 Is It a Complete AVL Tree(30 分)
  2. ACM学习历程—HDU1584 蜘蛛牌(动态规划 &amp;&amp; 状态压缩 || 区间DP)
  3. CSS 浏览器兼容
  4. Adding In-App Purchase to your iOS and OS X Applications
  5. JavaWeb_打包web应用war
  6. 9. 那些强悍的PHP一句话后门
  7. CI框架3.0版本以后,前后台分离的方法。
  8. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)
  9. ]Linq to EF 增删改查
  10. 在SQL中直接把查询结果转换为JSON数据