#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 "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(, , 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];
// 请求数据并进行处理
[self dealWithExpressMessage];
}
- (void)dealWithExpressMessage{
NSString *htmlString = @"http://wap.kuaidi100.com/wap_result.jsp?rand=20120517&id=zhongtong&fromWeb=null&&postid=718969747957";
NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:htmlString] encoding:NSUTF8StringEncoding error:nil];
NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
NSString *string = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
NSArray * array = [string componentsSeparatedByString:@"<p>·"];
// 分割后,获取有用的信息
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (int i = ; i < array.count; i++ ) {
if ((i != ) && (i != array.count - )) {
[mArr addObject:array[i]];
}
if (i == array.count -) {
NSArray *newArr = [array[i] componentsSeparatedByString:@"</form>"];
NSString *string = newArr[];
[mArr addObject:string];
}
}
// 将获取到的数据分开
for (NSString *subString in mArr) {
NSArray *array = [subString componentsSeparatedByString:@"<br />"];
NSString *timeString = array[];
NSArray *dateArray = [timeString componentsSeparatedByString:@";"];
[timeArr addObject:dateArray[]];
/* 将年月日与时分秒的时间分开
NSArray *spaTimeArr = [timeString componentsSeparatedByString:@" "];
NSArray *dateArray = [spaTimeArr[0] componentsSeparatedByString:@";"];
// 获取年月日的时间
[dateArr addObject:dateArray[1]];
//获取时分秒的时间
[timeArr addObject:spaTimeArr[1]];
*/
// NSLog(@" == %@",dateArray[1]);
NSString *address = array[];
NSArray *addressArr = [address componentsSeparatedByString:@"</p>"];
// 存放快递的信息
NSString *newString = [addressArr[] stringByReplacingOccurrencesOfString:@" " withString:@""];
[messsgeArr addObject:newString];
NSLog(@"==%@",newString);
}
// NSLog(@"===== %@",messsgeArr);
}
#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
{
UITableView *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