搜索框的使用

//

//  ViewController.m

//  联系人管理

//

//  Created by 文华 on 15/12/15.

//  Copyright © 2015 文华. All rights reserved.

//


#import "ViewController.h"

#import "Contact.h"

#import "ShowViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableview;

//@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;

//@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDisplay;

//@property (strong, nonatomic) IBOutlet UISearchController *searchDisplay;


@end


@implementation ViewController

//模态窗口直接打开就行,不需要函数

- (IBAction)addContact:(UIBarButtonItem *)sender {

    //*   模态窗口

//   EditViewController *editVC=[EditViewController new];

//   

//    [self presentViewController:editVC animated:YES completion:nil];

   

    

    

    


    

    //UIStoryboardSegue* segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"qq" source:self destination:editVC];

    //NSLog(@"%@",segue.identifier);

    

    //展示show窗口

    //[self performSegueWithIdentifier:@"add" sender:self];

    

}

#pragma mark 回传数据的代理方法

-(void)backWithContact:(Contact *)contactPsn IsEditing:(BOOL)isEditing

{

    

    if (!isEditing) {

       [self.contacts addObject:contactPsn];

    }

    

    [self.tableview reloadData];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.contacts=[[NSMutableArray alloc]init];

    self.searchResult=[[NSMutableArray alloc]init];

    for (int i=0; i<3; i++) {

        Contact *contact=[Contact new];

        contact.name=[NSString stringWithFormat:@"person%d",i+1];

        contact.tel=[NSString stringWithFormat:@"1850036%04d", arc4random_uniform(1000)];

        [self.contacts addObject:contact];

        

    }

    

    

   

    //设置搜索框

    //Pass nil if you wish to display search results in the same view that you are searching.

    self.searchDisplay=[[UISearchController alloc]initWithSearchResultsController:nil];

    //该对象用来更新数据

    self.searchDisplay.searchResultsUpdater=self;

    //[self.searchDisplay.searchBar sizeToFit];

    //设置是否隐藏导航栏

    self.searchDisplay.hidesNavigationBarDuringPresentation=YES;

    //self.searchDisplay.searchBar.showsCancelButton=YES;

    CGRect frame=self.tableview.tableHeaderView.frame;

    frame.size.width=10;

    //self.tableview.tableHeaderView.frame=frame;

    //设置表格头部即为搜索框

    self.tableview.tableHeaderView=self.searchDisplay.searchBar;

    

    //self.searchDisplay.searchBar

    //显示取消按钮

    [self.searchDisplay.searchBar setShowsCancelButton:YES animated:NO];

    

    self.searchDisplay.dimsBackgroundDuringPresentation=false;

    

   

   

    self.tableview.delegate=self;

    self.tableview.dataSource=self;

    self.searchDisplay.delegate=self;

    //self.searchDisplay.delegate=self;

    //self.searchbar.delegate=self;

    //self.searchDisplay.searchResultsUpdater

   // NSLog(@"sss");

}

/*

UISearchControllersearchBar中的内容一旦发生变化, 就会调用该方法-updateSearchResultsForSearchController. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.


更新UITableView

引入UISearchController之后, UITableView的内容也要做相应地变动: cell中要呈现的内容是allCities, 还是filteredCities.

这一点, 可以通过UISearchControlleractive属性来判断, 即判断输入框是否处于active状态.

UITableView相关的很多方法都要根据active来做判断:

 */

//考虑搜索结果

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if (!self.searchDisplay.active) {

        

        return self.contacts.count;

    } else {

        return self.searchResult.count;

    }

    

}


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

{

    static NSString *cellID=@"contactCell";

    //1,从重用池中找不用的cell对象

    UITableViewCell *cell=[self.tableview dequeueReusableCellWithIdentifier:cellID];

    //2,如果没有救自己创建

    if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

        

    }

    Contact *contact=[Contact new];

    //3,设置cell的属性

     if (!self.searchDisplay.active) {

         contact=self.contacts[indexPath.row];

     }

    else

    {

        contact=self.searchResult[indexPath.row];

    }

    //Contact *contact=self.contacts[indexPath.row];

    cell.textLabel.text=contact.name;

    cell.detailTextLabel.text=contact.tel;

    cell.detailTextLabel.textAlignment=NSTextAlignmentLeft;

    cell.detailTextLabel.textColor=[UIColor grayColor];

    //NSLog(@"%@",contact.tel);

 

    return cell;

}

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

{

    return 40;

}

#pragma mark - delegate

#pragma mark 选中单元格时候的动作

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

{

//    if (tableView==self.tableview) {

//        self.IsSearchResult=NO;

//    }

//    else

//    {

//        self.IsSearchResult=YES;

//    }

    NSLog(@"222");

    [self performSegueWithIdentifier:@"show" sender:self];

    

}

#pragma mark segue跳转前的准备工作

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

{

    NSLog(@"连线=%@",segue.identifier);

    if ([segue.identifier isEqualToString:@"add"]) {

        //NSLog(@"****");

        EditViewController *editVC=segue.destinationViewController;

        editVC.delegate=self;

       // NSLog(@"连线");

    }

    else if([segue.identifier isEqualToString:@"show"])

    {

        

        ShowViewController *showVC=segue.destinationViewController;

        NSIndexPath *indexpath= [self.tableview indexPathForSelectedRow];

        NSLog(@"indexpath.row=%ld",indexpath.row);

        


        

      if (self.searchDisplay.active)

      {

            showVC.contactPsn=self.searchResult[indexpath.row];

        }

        else

    {

        showVC.contactPsn=self.contacts[indexpath.row];

    }

    }

}



//#pragma mark - searchBar代理方法

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

//{

//   // NSLog(@"%@",self.searchbar.text);

//}

//


/*

 下面的方法并没有执行

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar

{

    NSLog(@"searchBarTextDidEndEditing");

    self.IsSearchResult=YES;

    [self.searchDisplay.searchBar resignFirstResponder];

}



-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{

    NSLog(@"searchBarTextDidBeginEditing");

    self.IsSearchResult=YES;

    [self.searchDisplay.searchBar becomeFirstResponder];

}

//-(void)didPresentSearchController:(UISearchController *)searchController

//{

//    

//}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    NSLog(@"searchBarSearchButtonClicked");

    

    self.IsSearchResult=YES;

}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

         NSLog(@"searchBarCancelButtonClicked");

    self.IsSearchResult=NO;

}

 */

#pragma mark - UISearchResultsUpdating 代理协议

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //self.searchDisplay.searchBar

    [self.searchResult removeAllObjects];

   

    NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[c] %@", self.searchDisplay.searchBar.text];

    self.searchResult = [[self.contacts filteredArrayUsingPredicate:searchPredicate] mutableCopy];

    dispatch_async(dispatch_get_main_queue(), ^{

        [self.tableview reloadData];

    });

}

/*UISearchController的移除

viewWillDisappear中要将UISearchController移除, 否则切换到下一个View, 搜索框仍然会有短暂的存在.*/

//不确定该方法是否有用

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    if (self.searchDisplay.active) {

        self.searchDisplay.active = NO;

        [self.searchDisplay.searchBar removeFromSuperview];

    }

}


@end


Kibana搜索栏是一个强大的工具,用于在Elasticsearch中进行查询和过滤数据。它允许您构建各种类型的查询,以便快速找到所需的信息。下面是一些常见的使用方法: 1. 基本搜索:在搜索栏中输入关键字或短语,Kibana将在所有字段中进行匹配搜索。 2. 过滤搜索:使用过滤器来缩小搜索范围。您可以通过字段名称和相应的值来创建过滤器,例如"status:success"将仅显示状态为成功的结果。 3. 范围搜索:使用范围运算符(如">"、"<"、">="、"<=")来搜索具有特定值范围的字段。例如,"response_time:>500"将只显示响应时间大于500毫秒的结果。 4. 通配符搜索:使用通配符符号(*)来模糊匹配字段的值。例如,"user_name:john*"将显示以"john"开头的所有用户。 5. 布尔搜索:使用AND、OR、NOT等布尔运算符来组合多个条件进行搜索。例如,"status:success AND response_time:>500"将显示状态为成功且响应时间大于500毫秒的结果。 6. 正则表达式搜索:使用正则表达式来匹配字段值。例如,"message:/error\d+/"将显示包含以"error"开头并后跟一个或多个数字的消息。 7. 字段搜索:使用字段名称进行搜索。在搜索栏中输入字段名称,然后选择要匹配的值。这将限制搜索结果只显示具有特定字段值的文档。 这些是Kibana搜索栏的一些基本用法。您还可以在查询编辑器中使用高级查询语法来进一步定制和精确搜索。希望对您有所帮助!如果您有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值