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异步加载)

上一篇:ElasticSearch实战-日志监控平台


下一篇:C++基础——模拟事务 (2)Composite模式