#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> { // NSMutableArray *dateArr; NSMutableArray *timeArr;// 时间 NSMutableArray *messsgeArr; // 物流信息 UITableView * _tableView; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // self.view.backgroundColor = [UIColor lightGrayColor]; // dateArr = [[NSMutableArray alloc] init]; 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]; // 请求数据并进行处理 NSString *htmlString = @"http://wap.kuaidi100.com/wap_result.jsp?rand=20120517&id=zhongtong&fromWeb=null&&postid=718969747957"; // NSString *htmlString = @"http://wap.kuaidi100.com/wap_result.jsp?rand=20120517&id=shentong&fromWeb=null&&postid=718969747957"; // NSString *htmlString = @"http://wap.kuaidi100.com/wap_result.jsp?rand=20120517&id=shenong&fromWeb=null&&postid=78969747957"; [self dealWithExpressMessage:htmlString]; } - (void)dealWithExpressMessage:(NSString*)htmlString{ NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:htmlString] encoding:NSUTF8StringEncoding error:nil]; NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; NSString *string = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; // NSLog(@"************%@",string); // 判断链接是否出错 NSArray *linkError = [string componentsSeparatedByString:@"404错误"]; if (linkError.count > 1) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"信息有误" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alertView show]; return; } // 判断快递的信息是否有误,是则提示 NSArray *isError = [string componentsSeparatedByString:@"我要报错"]; if (isError.count == 2) { NSLog(@"======"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"信息有误" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alertView show]; return; } // 获取快递信息 NSArray * array = [string componentsSeparatedByString:@"<p>·"]; // 分割后,获取有用的信息 NSMutableArray *mArr = [[NSMutableArray alloc] init]; for (int i = 0; i < array.count; i++ ) { if ((i != 0) && (i != array.count - 1)) { [mArr addObject:array[i]]; } if (i == array.count -1) { NSArray *newArr = [array[i] componentsSeparatedByString:@"</form>"]; NSString *string = newArr[0]; [mArr addObject:string]; } } // 将获取到的数据分开 for (NSString *subString in mArr) { NSArray *array = [subString componentsSeparatedByString:@"<br />"]; NSString *timeString = array[0]; NSArray *dateArray = [timeString componentsSeparatedByString:@";"]; [timeArr addObject:dateArray[1]]; /* 将年月日与时分秒的时间分开 NSArray *spaTimeArr = [timeString componentsSeparatedByString:@" "]; NSArray *dateArray = [spaTimeArr[0] componentsSeparatedByString:@";"]; // 获取年月日的时间 [dateArr addObject:dateArray[1]]; //获取时分秒的时间 [timeArr addObject:spaTimeArr[1]]; */ // NSLog(@" == %@",dateArray[1]); NSString *address = array[1]; NSArray *addressArr = [address componentsSeparatedByString:@"</p>"]; // 存放快递的信息 NSString *newString = [addressArr[0] stringByReplacingOccurrencesOfString:@" " withString:@""]; [messsgeArr addObject:newString]; // NSLog(@"==%@",newString); } // NSLog(@"===== %@",messsgeArr); } #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