搜索條搜索

AppDelegate.m

ViewController *theVc = [[ViewController alloc]init];

UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];

self.window.rootViewController = theNav;


ViewController.m"

<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

{

//創建表格

UITableView? ? *theTableView;

//創建數組

NSMutableArray *theArray;

NSMutableArray *theNewArray;

//創建搜索條

UISearchBar *theSearchBar;

}


//========================

////創建導航標題

self.title = @"搜索條";

//創建搜索條控件并設置位置

theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];

//添加協議

theSearchBar.delegate = self;

//顯示取消按鈕

theSearchBar.showsCancelButton = YES;

//? ? //添加取消按鈕(焦點)

//? ? [theSearchBar becomeFirstResponder];

[self.view addSubview:theSearchBar];

//初始化表格

theTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height)];

//添加表格協議

theTableView.dataSource = self;

theTableView.delegate? = self;

//添加到視圖上

[self.view addSubview:theTableView];

//1? 4

theArray = [NSMutableArray arrayWithObjects:@"張浩寧",@"李凱",@"井秋香",@"王劍",@"王一鳴",@"王瑞宇", nil];

theNewArray = [theArray mutableCopy];


//=======================

#pragma mark - UITableViewDataSource

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

return theArray.count;

}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

//創建復用池

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

//創建正標題

cell.textLabel.text = theArray[indexPath.row];

return cell;

}

#pragma -

#pragma mark - UISearchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{

[self showdata];

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

if([searchBar canResignFirstResponder])

[searchBar resignFirstResponder];

[self showdata];

}

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

[self search:searchText];

}

#pragma mark -

#pragma mark private methods

-(void)showdata{

theArray = [theNewArray mutableCopy];

[theTableView reloadData];

}

-(void)search:(NSString*)searchtext{

NSMutableArray *tmp = [NSMutableArray array];

//篩選數據? 把輸入的字母全部轉換為大寫(讓判斷不區分大小寫)

NSString *str = [theSearchBar.text uppercaseString];

//遍歷人名數組 哈希算法:hasPrefix

for (NSString *s in theArray) {

//判斷是否以輸入的字母開頭

if([[s uppercaseString] hasPrefix:str] )

{

[tmp addObject:s];

}

}

//將新的內容添加到新的數組中并同時賦值給theArray這個數組對象

theArray = [NSMutableArray arrayWithArray:tmp];

//刷新表格

[theTableView reloadData];

}


//==============================================第二種方法 完善======================================

ViewController.m

<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

{

NSArray *_allData;

//搜索條

UISearchBar *search;

//搜索控制器

UISearchDisplayController *displayCon;

//搜索結果

NSArray *_resultArr;

}

@property (nonatomic,strong)UITableView *table;

//重寫get方法

-(UITableView *)table{

if (!_table) {

_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];

_table.dataSource = self;

_table.delegate = self;

}

return _table;

}

//===================================

//模擬數據

_allData = [NSArray arrayWithObjects:@"驚奇",@"許大川",@"王劍",@"杜建軍",@"小鵬", nil];

[self.view addSubview:self.table];

//初始化搜索條

search = [[UISearchBar alloc]init];

//設置提示文字

search.placeholder = @"搜索姓名";

search.delegate = self;

//初始化搜索控制器

displayCon = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];

//設置代理

displayCon.searchResultsDataSource =self;

displayCon.searchResultsDelegate =self;

//設置頭視圖

self.table.tableHeaderView = search;

//? ? _resultArr = [[NSMutableArray alloc]init];

//===================================

//返回多少行數據

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

if (self.table == tableView) {

return _allData.count;

}else{

return _resultArr.count;

}

}

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

static NSString *cellId = @"cellid";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (!cell) {

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

}

if (self.table == tableView) {

cell.textLabel.text = _allData[indexPath.row];

}else{

cell.textLabel.text = [_resultArr objectAtIndex:indexPath.row];

}

return cell;

}

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

NSMutableArray *tmp = [NSMutableArray array];

//篩選數據? 把輸入的字母全部轉換為大寫(讓判斷不區分大小寫)

NSString *str = [search.text uppercaseString];

//遍歷人名數組

for (NSString *s in _allData) {

//判斷是否以輸入的字母開頭

if([[s uppercaseString] hasPrefix:str] ){

[tmp addObject:s];

}

}

_resultArr = [NSArray arrayWithArray:tmp];

[self.table reloadData];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,092評論 3 38
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 961評論 0 0
  • 1、searchBar 本例子實現布局:上面是一個navigationController,接下來一個search...
    lilinjianshu閱讀 3,480評論 1 8
  • 哦吼吼,又研究了幾天,把FMDB這個封裝好的數據庫搞定了,寫了個簡單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 569評論 0 0
  • 一個普通的searchBar的使用 寫在前面 這兩天想給app加一個搜索功能,打算采用系統自帶的UISearch等...
    早起的蟲兒子被鳥吃閱讀 1,721評論 0 2