美文网首页
UISearchBar 搜索条

UISearchBar 搜索条

作者: nalis风 | 来源:发表于2016-07-19 12:25 被阅读103次

    1。Options

    ShowsSearchResultsButton:勾选该复选框后,将会在搜索文本框的右端显示一个如“三”形状的图标按钮,用户可通过淡季该按钮激发特定的事件。

    ShowsBookmarksButton:显示Cancel按钮

    2.ShowsscopeBar与ScopeTitles

    如果勾选ScB,在搜索框下方先是一个分段条。接下来的ST将用于设置各分段的标题。如ST包含三个列表项:aaa,bbb,ccc。则表明搜索框下方先是一个分为三段的分段条。

    UISearchBarDelegate协议:

    searchBar:textDidChange:文本改变时激发

    searchBarBookmarkButtonClicked:点击搜索条的书签按钮

    searchBarCancelButtonClocked:

    ...Search...

    searchBarResultsListButtonClicked:点击搜索条上的 查询结果按钮

    searchBarLseletedScopeButtonIndexDidChange:当用户点击分段条上的分段按钮时激发

    .h---------------------------------------------

    @implementationViewController

    boolisSearch;

    - (void)viewDidLoad {

    [superviewDidLoad];

    isSearch=NO;

    self.tableData=[NSArrayarrayWithObjects:

    @"法国",

    @"新西兰",

    @"英国",

    @"希腊",nil];

    self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(5,55,400,500)style:UITableViewStylePlain];

    self.tableView.delegate=self;

    self.tableView.dataSource=self;

    [self.viewaddSubview:self.tableView];

    self.searchBar=[[UISearchBaralloc]initWithFrame:CGRectMake(0,5,418,48)];

    self.searchBar.showsCancelButton=YES;

    self.searchBar.showsSearchResultsButton=YES;

    self.searchBar.showsScopeBar=YES;

    self.searchBar.showsBookmarkButton=YES;

    self.searchBar.delegate=self;

    [self.viewaddSubview:self.searchBar];

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

    }

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

    if(isSearch) {

    returnself.searchData.count;

    }

    returnself.tableData.count;

    }

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

    staticNSString* cellId=@"cellId";

    UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellId];

    if(!cell) {

    //创建表格行

    cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

    }

    NSIntegerrowNo=indexPath.row;

    if(isSearch) {

    //使用searchData作为表格显示的数据

    cell.textLabel.text=self.searchData[rowNo];

    }else{

    //否则使用原始的tableData作为表格显示的数据

    cell.textLabel.text=self.tableData[rowNo];

    }

    returncell;

    }

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

    //取消搜索状态

    isSearch=NO;

    [self.tableViewreloadData];

    }

    //UISearchBarDelegate定义的方法,当搜索文本框内的文本改变时激发该方法

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

    //调用filterBySubstring:方法执行搜索

    [selffilterBySubstring:searchBar.text];

    //放弃作为第一响应者,关闭键盘

    [searchBarresignFirstResponder];

    }

    -(void)filterBySubstring:(NSString*)subStr{

    //设置搜索状态

    isSearch=YES;

    //定义搜索为此

    NSPredicate* pred=[NSPredicatepredicateWithFormat:@"self contains[c] %@",subStr];

    //使用为此过滤NSArray

    self.searchData=[self.tableDatafilteredArrayUsingPredicate:pred];

    //让表格重新加载数据

    [self.tableViewreloadData];

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

    10.17.2 使用UISearchDisplayController

    下拉列表显示搜索结果:

    相关文章

      网友评论

          本文标题:UISearchBar 搜索条

          本文链接:https://www.haomeiwen.com/subject/zlmmjttx.html