ios tableview搜索

来源:互联网 发布:淘宝达人发布优质帖子 编辑:程序博客网 时间:2024/06/05 08:08

跟安卓差不多的机制,mvc。搜索嘛  就是改变数据源  刷新下白。

//

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

{

    [superviewDidLoad];

    CGFloat width=mainWidth;

    CGFloat height=mainHeight;

    self.navigationItem.title=@"就业信息";

    _datalist=[[NSMutableArrayalloc]init];

     _searchdata=[[NSMutableArrayalloc]init];

    _data=[[NSMutableArrayalloc]init];

    // Do any additional setup after loading the view.

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

    _table=[[UITableViewalloc]initWithFrame:CGRectMake(0,0, width, height) style:UITableViewStylePlain];

    _table.delegate=self;

    _table.dataSource=self;

    UIBarButtonItem *search=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefreshtarget:selfaction:@selector(search)];

    self.navigationItem.leftBarButtonItem=search;

    [selfaddSearchBar];

    [[UINavigationBarappearance] setBackgroundColor:[UIColorgreenColor]];

    [self.view addSubview:_table];

}


-(void)addSearchBar{

    CGRect searchBarRect=CGRectMake(0,0, self.view.frame.size.width,40);

    _search=[[UISearchBaralloc]initWithFrame:searchBarRect];

    _search.placeholder=@"Please input key word...";

    //_searchBar.keyboardType=UIKeyboardTypeAlphabet;//键盘类型

    //_searchBar.autocorrectionType=UITextAutocorrectionTypeNo;//自动纠错类型

    //_searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;//哪一次shitf被自动按下

    _search.showsCancelButton=YES;//显示取消按钮

    //添加搜索框到页眉位置

    _search.delegate=self;

    self.table.tableHeaderView=_search;

}

-(void)search{

    [_datalistremoveAllObjects];

    [self load];

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [_searchdataremoveAllObjects];

    

    [selfsearchDataWithKeyWord:_search.text];

    NSLog(@"here");

    

    [_searchresignFirstResponder];//放弃第一响应者对象,关闭虚拟键盘

}

-(void)searchDataWithKeyWord:(NSString *)keyWord{

    _isSearching=YES;

    for(int i=0;i<[_datacount];i++){

        JobBean *bean=[_dataobjectAtIndex:i];

        if([bean.jobTitlecontainsString:keyWord]){

            [_searchdata addObject:bean];

        }

        _datalist=_searchdata;

    }

    

    //刷新表格

    [self.tablereloadData];

}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

    _isSearching=NO;

    _search.text=@"";

    _datalist=_data;

     [_searchresignFirstResponder];//放弃第一响应者对象,关闭虚拟键盘

    [_searchdataremoveAllObjects];

    [self.tablereloadData];

}

#pragma mark 输入搜索关键字

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if([_search.textisEqual:@""]){

        _isSearching=NO;

        [self.tablereloadData];

        return;

    }

    [selfsearchDataWithKeyWord:_search.text];

}


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

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section{

    if(tableView==self.table){

    return [_datalistcount];

    }else{

        return _searchdata.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=[tableViewdequeueReusableCellWithIdentifier:ID];

        if (cell==nil) {

                 cell=[[myCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

             }

    

         //3.设置cell的数据

             NSInteger row = [indexPath row];

    if (tableView==self.table) {

        JobBean* bean=[_datalistobjectAtIndex:row];

        NSString *s = bean.jobTitle;

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

        CGSize size = CGSizeMake(320,2000);

        CGSize labelsize = [ssizeWithFont:font constrainedToSize:sizelineBreakMode:UILineBreakModeWordWrap];

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

        cell.myLable.backgroundColor = [UIColorclearColor];

        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=[_datalistobjectAtIndex:row];

    NSString *s = bean.jobTitle;

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

    CGSize size = CGSizeMake(320,2000);

    CGSize labelsize = [ssizeWithFont:font constrainedToSize:sizelineBreakMode:UILineBreakModeWordWrap];

    return labelsize.height+10;

}


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

    

    NSInteger row=[indexPath row];

    jobDetilViewController* detil=[[jobDetilViewControlleralloc]init];

    JobBean*  bean=[_datalistobjectAtIndex:row];

    detil.title=bean.jobTitle;

    detil.DetailUrl=bean.jobDetilurl;

    [self.navigationControllerpushViewController:detil animated:YES];

    

}



-(void)load{

    

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

    NSURL *url = [NSURLURLWithString:urlstring];

    NSData *data = [NSDatadataWithContentsOfURL:url];

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

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

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

        TFHppleElement * element = [elementsobjectAtIndex:i];

        NSString *links=[element raw];

        NSArray* href=[linkscomponentsSeparatedByString:@" "];

        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=[[JobBeanalloc]init];

        bean.jobTitle=[element text];

        bean.jobDetilurl=reallink;

        [_datalist addObject:bean];


    }

    _data=[[NSMutableArrayalloc]initWithArray:_datalist];

    

    [_tablereloadData];

}

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

    NSURL *url = [NSURLURLWithString:value];

    NSData *data = [NSDatadataWithContentsOfURL:url];

    //解决中文乱码,GBK

    NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

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

    return retStr;  

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

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