ios cell自适应高度

//

//  rootViewController.m

//  QduniversityJobhelper

//

//  Created by niuxinghua on 14-12-17.

//  Copyright (c) 2014 niuxinghua. All rights reserved.

//


#import "rootViewController.h"

#import "Config.h"

#import "AFNetworking.h"

#import "TFHpple.h"

#import "TFHppleElement.h"

#import "XPathQuery.h"

#import "TFHpple.h"

#import "JobBean.h"

#import "myCell.h"

#import "jobDetilViewController.h"

@interface rootViewController ()


@end


@implementation rootViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    CGFloat width=mainWidth;

    CGFloat height=mainHeight;

    _datalist=[[NSMutableArray alloc]init];

    // Do any additional setup after loading the view.

    _button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    _button.frame=CGRectMake(width/2-40, 80, 80, 40);

    [_button setTitle:@"load" forState:UIControlStateNormal];

    [_button addTarget:self action:@selector(load) forControlEvents:UIControlEventTouchUpInside];

    _text=[[UITextField alloc]initWithFrame:CGRectMake(0, 120, width, height-120)];

    _table=[[UITableView alloc]initWithFrame:CGRectMake(0, 120, width, height-120) style:UITableViewStylePlain];

    _table.delegate=self;

    _table.dataSource=self;

    [self.view addSubview:_button];

    [self.view addSubview:_table];

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section{

    return [_datalist count];

}

/**

 @method 获取指定宽度情况ixa,字符串value的高度

 @param value 待计算的字符串

 @param fontSize 字体的大小

 @param andWidth 限制字符串显示区域的宽度

 @result float 返回的高度

 */



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //1.到缓存中去取cell

        static NSString *ID=@"ID";

         //2.没有则创建cell

        myCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

        if (cell==nil) {

                 cell=[[myCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

             }

    

         //3.设置cell的数据

             NSInteger row = [indexPath row];


    JobBean* bean=[_datalist objectAtIndex:row];

    NSString *s = bean.jobTitle;

    UIFont *font = [UIFont fontWithName:@"Arial" size:20];

    CGSize size = CGSizeMake(320,2000);

    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

    //cell.frame.size.height=80;

    cell.myLable.backgroundColor = [UIColor clearColor];

    cell.myLable.text = bean.jobTitle;

    cell.myLable.textAlignment=UITextAlignmentLeft;

    cell.myLable.numberOfLines=0;

   // JobBean* bean=[_datalist objectAtIndex:row];

    //cell.myLable.frame=CGRectMake(10, -20, 320, 80);

    [cell setLableFrame:CGRectMake(10, 0, 300, labelsize.height)];

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row=[indexPath row];

    JobBean *bean=[_datalist objectAtIndex:row];

    NSString *s = bean.jobTitle;

    UIFont *font = [UIFont fontWithName:@"Arial" size:20];

    CGSize size = CGSizeMake(320,2000);

    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

    return labelsize.height+10;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    NSInteger row=[indexPath row];

    jobDetilViewController* detil=[[jobDetilViewController alloc]init];

    JobBean*  bean=[_datalist objectAtIndex:row];

    detil.title=bean.jobTitle;

    detil.DetailUrl=bean.jobDetilurl;

    [self.navigationController pushViewController:detil animated:YES];

    

}



-(void)load{

    

    NSString *urlstring= @"http://career.ouc.edu.cn/recruitment/campus/";// 此处网址不对,只是示意可以生成一个动态的urlstring

    NSURL *url = [NSURL URLWithString:urlstring];

    NSData *data = [NSData dataWithContentsOfURL:url];

    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:data];

    NSArray * elements  = [doc searchWithXPathQuery:@"//a"];

    for (int i=10; i<[elements count]-27; i++) {

        TFHppleElement * element = [elements objectAtIndex:i];

        NSString *links=[element raw];

        NSArray* href=[links componentsSeparatedByString:@" "];

        NSString *link=[href objectAtIndex:1];

        NSString *link1=[link substringFromIndex:6];

        int length=[link1 length]-1;

        NSString *reallink=[link1 substringToIndex:length];

       // NSLog(@"%@",[element text]);

        //NSLog(@"%@",reallink);

        JobBean *bean=[[JobBean alloc]init];

        bean.jobTitle=[element text];

        bean.jobDetilurl=reallink;

        [_datalist addObject:bean];


    }

    

    

    [_table reloadData];

}

-(NSString *)urlString:(NSString *)value{

    NSURL *url = [NSURL URLWithString:value];

    NSData *data = [NSData dataWithContentsOfURL:url];

    //解决中文乱码,GBK

    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];

    return retStr;  

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值