//
// 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