UISearBarController

来源:互联网 发布:内存优化级别禁用好吗 编辑:程序博客网 时间:2024/06/03 21:41

iOS8以后出来的,Interface Builder是没有的,只能纯代码。
直接上代码了,蛮简单的
具体你要实现什么,逻辑自己写

////  ViewController.m//  UISearBar////  Created by 易云时代 on 2017/8/21.//  Copyright © 2017年 笑伟. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating>@property (nonatomic, strong) UISearchController *serachC;@property (nonatomic, strong) UITableView *table;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WITH, SCREEN_HEIGHT)];    _table.delegate = self;    _table.dataSource = self;    [self.view addSubview:_table];    _serachC = [[UISearchController alloc]initWithSearchResultsController:nil];    _serachC.searchResultsUpdater = self;//self 为更新结果对象    self.table.tableHeaderView = _serachC.searchBar;}#pragma mark TableViewDelegateAndDataSource-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 50;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 30;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pp"];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pp"];    }    return cell;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 10;}-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{    return 2;}#pragma mark UISearchBarDelegate-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{    [self updateSearchResultsForSearchController:self.serachC];}#pragma mark UISearchResultsUpdating-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{    NSLog(@"%@",_serachC.searchBar.text);    [_table reloadData];}
原创粉丝点击