代码改变世界

iOS UILabel根据文字获取高度及UITableCell动态获取高度(以截取快递信息为例)

2015-11-15 11:05  甘雨路  阅读(482)  评论(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>
{
//    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];
    
    // 请求数据并进行处理
    [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>&middot"];
    // 分割后,获取有用的信息
    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
{
    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(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 + 5;
    cell.frame = rect;

    return cell;
}


@end