iOS 获取快递物流信息(GCD异步加载)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" #define cellWidth [UIScreen mainScreen].bounds.size.width
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> {
NSString *expressCode;//快递公司的编号
NSString *expressNumber;//快递的单号 NSMutableArray *timeArr;// 时间
NSMutableArray *messsgeArr; // 物流信息
UITableView * _tableView;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad]; timeArr = [[NSMutableArray alloc] init];
messsgeArr = [[NSMutableArray alloc] init]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , cellWidth, [UIScreen mainScreen].bounds.size.height - ) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_tableView];
//去掉多余的cell
_tableView.tableFooterView = [[UIView alloc] init]; expressCode = @"shentong";
expressNumber = @"";
// 获取快递信息的相关链接
NSString *path = [[NSString alloc] initWithFormat:@"http://www.kuaidi100.com/query?type=%@&postid=%@&id=1&valicode=&temp=0.42161923577077687",expressCode,expressNumber];
//处理快递的信息
[self dealWithExpressMessage:path]; }
/**
* 处理快递的信息
*
* @param htmlString 获取快递信息的相关链接
*/
- (void)dealWithExpressMessage:(NSString*)htmlString{
// GCD开启线程加载数据
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
//异步记载
dispatch_async(queue, ^{
NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:htmlString] encoding:NSUTF8StringEncoding error:nil];
NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//将二进制转化为字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:htmlData options: error:nil];
//判断快递的信息是否正确
NSString *isOk = dic[@"message"];
if ([isOk isEqualToString:@"ok"]) {
// 获取相关信息
NSArray *arr = dic[@"data"];
for (NSDictionary *infoDic in arr) {
NSString *time = infoDic[@"time"];
NSString *context = infoDic[@"context"];
[timeArr addObject:time];
[messsgeArr addObject:context];
}
//返回主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
}
});
} #pragma mark -- tableView 的数据配置 --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return timeArr.count;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"cell";
tableView.separatorStyle = UITableViewCellSelectionStyleNone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.userInteractionEnabled = NO;
for (UIView *subView in cell.subviews) {
[subView removeFromSuperview];
}
cell.backgroundColor = [UIColor lightGrayColor];
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , cellWidth, )];
timeLabel.textColor = [UIColor blueColor];
timeLabel.font = [UIFont systemFontOfSize:];
timeLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:timeLabel];
timeLabel.text = timeArr[indexPath.row]; UILabel *messageLabel = [[UILabel alloc] init];
[cell addSubview:messageLabel];
messageLabel.text = messsgeArr[indexPath.row]; messageLabel.numberOfLines = ;
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor blackColor];
UIFont *font = [UIFont fontWithName:@"Arial" size:];
messageLabel.font = font;
CGSize constraint = CGSizeMake(cellWidth - , );
CGSize size = [messageLabel.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// CGSize labelSize = [messageLabel.text boundingRectWithSize:boundSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
messageLabel.frame = CGRectMake(, , size.width,size.height);
CGRect rect = cell.frame;
rect.size.height = timeLabel.frame.size.height + messageLabel.frame.size.height + ;
cell.frame = rect; return cell;
} @end

iOS 获取快递物流信息(GCD异步加载)的更多相关文章
- [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading
上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...
- BitmapImage处理网络图片,例如阿里云获取的图片。异步加载到需要显示的控件上。提升速度非常明显。
想直接把网络图片赋给控件,又要下载又要缓存,速度非常慢.不流畅. 需要进行处理,异步加载会显著提升速度.方法如下: public static BitmapImage ByteArrayToBitma ...
- GCD异步加载网络图片
//image dispatch_queue_t network_queue; network_queue = dispatch_queue_create("com.myapp.networ ...
- 关于ios异步加载图片的几个开源项目
一.HjCache 原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...
- iOS网络编程(三) 异步加载及缓存图片---->SDWebImage
@SDWebImage提供一个UIImageView的类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. @SDWebImage的导入1.https:// ...
- IOS学习之路二十三(EGOImageLoading异步加载图片开源框架使用)
EGOImageLoading 是一个用的比较多的异步加载图片的第三方类库,简化开发过程,我们直接传入图片的url,这个类库就会自动帮我们异步加载和缓存工作:当从网上获取图片时,如果网速慢图片短时间内 ...
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- Python爬虫获取异步加载站点pexels并下载图片(Python爬虫实战3)
1. 异步加载爬虫 对于静态页面爬虫很容易获取到站点的数据内容,然而静态页面需要全量加载站点的所有数据,对于网站的访问和带宽是巨大的挑战,对于高并发和大访问访问量的站点来说,需要使用AJAX相关的技术 ...
- ios UITableView 异步加载图片并防止错位
UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...
随机推荐
- Frenetic Python实验(二)
实验3 packet_in_out 目的:模拟一个普通的双端口中继器. This application implements a very simple 2 port repeater where ...
- Redis 笔记与总结6 Redis 高级应用之 事务处理、持久化操作、pub_sub、虚拟内存
3.事务处理 redis 对事务的支持目前还比较简单. redis 只能保证一个 client 发起的事务中的命令可以连续的执行,而中间不会插入其他 client 的命令. 由于 redis 是单线 ...
- 自动更新开奖数据的excel文件,供大家下载
自动更新开奖数据的excel文件,供大家下载 2010-03-14 20:22 228492人阅读打印来源:乐彩网 作者:eren 很多人拥有自制excel电子表格,常要更新最基本的开奖信息.如有多期 ...
- 各种常用的JSON接口,开动你的大脑你就可以做出各种应用,值得收藏
各种常用的JSON接口,开动你的大脑你就可以做出各种应用,值得收藏 浏览:1412 发布日期:2014/01/27 分类:技术分享 这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用J ...
- Nginx Location 语法,与简单配置[转]
一、介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器 ...
- 关于集合的练习P235-1,2,3
第一题: import java.util.*; public class ListTest { public static void main(String[] args) { ArrayList& ...
- Introduction to Structured Data
https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...
- 在Delphi中如何动态创建dbf数据库(二)?
unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...
- comms.nottingham.ac.uk/learningtechnology
http://comms.nottingham.ac.uk/learningtechnology/
- SQLServer2008:助您轻松编写T-SQL存储过程(原创)【转】
本文主要介绍 SQLServerExpress2008不用第三方工具调试T-SQL语句,经过本文的介绍,用SQLSERVER2008 Manage studio 编写.调试T-SQL存储过程都将是 ...