代码改变世界

iOS 获取快递物流信息(GCD异步加载)

2015-12-14 14:07  甘雨路  阅读(778)  评论(0编辑  收藏  举报
#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(0, 20, cellWidth, [UIScreen mainScreen].bounds.size.height - 20) 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 = @"227132715137";
    // 获取快递信息的相关链接
    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, 0);
    //异步记载
    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:0 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 1;
}
- (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(5, 0, cellWidth, 20)];
    timeLabel.textColor = [UIColor blueColor];
    timeLabel.font = [UIFont systemFontOfSize:14];
    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 = 0;
    messageLabel.backgroundColor = [UIColor clearColor];
    messageLabel.textColor = [UIColor blackColor];
    UIFont *font = [UIFont fontWithName:@"Arial" size:16];
    messageLabel.font = font;
    CGSize constraint = CGSizeMake(cellWidth - 10, 20000);
    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(5, 20, size.width,size.height);
    CGRect rect = cell.frame;
    rect.size.height = timeLabel.frame.size.height + messageLabel.frame.size.height + 20;
    cell.frame = rect;
    
    return cell;
}

@end