UISearchController的簡單使用

思路: 把UISearchVC作為tableView的頭視圖错负,只是顯示一個(gè)搜索框甘耿,當(dāng)點(diǎn)擊搜索框時(shí),可輸入文字進(jìn)行搜索么翰,根據(jù)searchVC的active屬性來在tabelView上顯示數(shù)據(jù)青灼。searchVC的代理方法暴心,當(dāng)搜索框內(nèi)容變化時(shí)會(huì)調(diào)用,可根據(jù)輸入內(nèi)容篩選搜索結(jié)果在tabelView上顯示杂拨。

代理方法:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

效果圖:

效果圖.gif

代碼如下:

#import "MainViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource,UISearchResultsUpdating, UISearchBarDelegate>
@property (nonatomic, strong) NSMutableArray *allData;
@property (nonatomic, strong) NSMutableArray *searchData;
@property (nonatomic, strong) UITableView *myTable;
@property (nonatomic, strong) UISearchController *searchVC;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"搜索";
    
    self.allData = [NSMutableArray array];
    self.searchData = [NSMutableArray array];
    
    [self createTableView];
    [self createSearchVC];
    
    for (int i = 0; i < 100; i++) {
        
        [self.allData addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
}

- (void)createTableView{
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.myTable.dataSource = self;
    self.myTable.delegate = self;
    [self.view addSubview:self.myTable];
}

-(void)createSearchVC{
    self.searchVC = [[UISearchController alloc]initWithSearchResultsController:nil];
    //設(shè)置searchVC的代理
    self.searchVC.searchResultsUpdater = self;
    //點(diǎn)擊搜索時(shí)是否加半透明遮罩視圖
    self.searchVC.dimsBackgroundDuringPresentation = NO;
    self.searchVC.searchBar.delegate = self;
    //點(diǎn)擊搜索框時(shí)是否隱藏原來的導(dǎo)航欄
    self.searchVC.hidesNavigationBarDuringPresentation = YES;
    //設(shè)置搜索框上的文字
    self.searchVC.searchBar.placeholder = @"搜索";
    self.myTable.tableHeaderView = self.searchVC.searchBar;
    
    //下面這兩行是設(shè)置 searchVC上searchBar后面的cancle改為取消
//   [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"取消"];
    [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
    
}

//這個(gè)方法也能設(shè)置搜索框上的cancelBtn系顯示樣式
#pragma mark -UISearchBarDelegate
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
//    [searchBar setShowsCancelButton:YES animated:YES];
////    searchBar.showsCancelButton = YES;
//    for (id view in [[searchBar.subviews objectAtIndex:0] subviews]) {
//        if ([view isKindOfClass:[UIButton class]]) {
//
//            UIButton *btn = (UIButton *)view;
//            [btn setTitle:@"取消" forState:UIControlStateNormal];
//            break;
//        }
//    }
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//設(shè)置區(qū)域的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.searchVC.active) {
        return [self.searchData count];
    }else{
        return [self.allData count];
    }
}

//返回單元格內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *flag=@"cellFlag";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    }
    if (self.searchVC.active) {
        [cell.textLabel setText:self.searchData[indexPath.row]];
    }
    else{
        [cell.textLabel setText:self.allData[indexPath.row]];
    }
    //
    return cell;
}

#pragma mark -UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    NSString *searchText = self.searchVC.searchBar.text;
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", searchText];
    if ([self.searchData count] != 0) {
        [self.searchData removeAllObjects];
    }
    
    // 直接用賦值的方法专普,可變數(shù)組會(huì)變成不可變 這樣強(qiáng)轉(zhuǎn)searchData仍是不可變數(shù)組,再去調(diào)removeAllObjects會(huì)崩潰
//    self.searchData = (NSMutableArray *)[self.allData filteredArrayUsingPredicate:predicate];
//    錯(cuò)誤提示: [__NSArrayI removeAllObjects]: unrecognized selector sent to instance
    
    //以下三種方法是可以的
    [self.searchData addObjectsFromArray:[self.allData filteredArrayUsingPredicate:predicate]];
    
//    self.searchData = [[self.allData filteredArrayUsingPredicate:predicate] mutableCopy];

//    self.searchData = [NSMutableArray arrayWithArray:[self.allData filteredArrayUsingPredicate:predicate]];
    
    [self.myTable reloadData];
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末扳躬,一起剝皮案震驚了整個(gè)濱河市脆诉,隨后出現(xiàn)的幾起案子甚亭,更是在濱河造成了極大的恐慌,老刑警劉巖击胜,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亏狰,死亡現(xiàn)場離奇詭異,居然都是意外死亡偶摔,警方通過查閱死者的電腦和手機(jī)暇唾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辰斋,“玉大人策州,你說我怎么就攤上這事」蹋” “怎么了够挂?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長藕夫。 經(jīng)常有香客問我孽糖,道長,這世上最難降的妖魔是什么毅贮? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任办悟,我火速辦了婚禮,結(jié)果婚禮上滩褥,老公的妹妹穿的比我還像新娘病蛉。我一直安慰自己,他們只是感情好瑰煎,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布铺然。 她就那樣靜靜地躺著,像睡著了一般丢间。 火紅的嫁衣襯著肌膚如雪探熔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天烘挫,我揣著相機(jī)與錄音诀艰,去河邊找鬼。 笑死饮六,一個(gè)胖子當(dāng)著我的面吹牛其垄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播卤橄,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼绿满,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了窟扑?” 一聲冷哼從身側(cè)響起喇颁,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤漏健,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后橘霎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蔫浆,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年姐叁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓦盛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,567評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡外潜,死狀恐怖原环,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情处窥,我是刑警寧澤嘱吗,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站碧库,受9級特大地震影響柜与,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嵌灰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颅悉。 院中可真熱鬧沽瞭,春花似錦、人聲如沸剩瓶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽延曙。三九已至豌鹤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間枝缔,已是汗流浹背布疙。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留愿卸,地道東北人灵临。 一個(gè)月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像趴荸,于是被迫代替她去往敵國和親儒溉。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容