国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS之UISearchDisplayController搜索的使用方法

2020-02-19 15:51:08
字體:
來源:轉載
供稿:網友

iOS中的UISearchDisplayController用于搜索,但是在iOS中的搜索欄實現起來是比較簡單的,其實網上有很多參考文獻,但可靠并不是很多,下文是武林技術頻道小編和大家分享的,希望對你學習有幫助!

新建Navigation-based Project。打開.xib文件,拖一個Search Bar and Search DisplayController 對象到Table View對象上方,如下圖所示,選中File's Owner ,打開Connections面板:

201621891219772.jpg (750×420)

現在我們來創建Search Bar和SearchDisplay Controller的出口。打開Assistant Editor,按住ctrl鍵,將SearchDisplay Controller拖到ViewController 的頭文件中。創建一個名為searchDisplayController的出口,然后點Connect。

201621891243378.jpg (750×410)

同樣的方法為Search Bar創建連接。現在ViewController的頭文件看起來像這樣:

復制代碼 代碼如下:

#import

@interface RootViewController : UITableViewController {

UISearchDisplayController *searchDisplayController; UISearchDisplayController *searchBar;

NSArray *allItems;

NSArray *searchResults;

}

@property (nonatomic, retain) IBOutlet UISearchDisplayController *searchDisplayController;

@property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar;

@property (nonatomic, copy) NSArray *allItems;

@property (nonatomic, copy) NSArray *searchResults;

@end


你可能注意到,我初始化了兩個NSArray。一個用于作為數據源,一個用于保存查找結果。在本文中,我使用字符串數組作為數據源。繼續編輯.m文件前,別忘了synthesize相關屬性:

  • @synthesize searchDisplayController;
  • @synthesize searchBar;
  • @synthesize allItems;
  • @synthesize searchResults;

在viewDidLoad 方法中,我們構造了我們的字符串數組:

復制代碼 代碼如下:

- (void)viewDidLoad {

[super viewDidLoad];

// [self.tableView reloadData];

self.tableView.scrollEnabled = YES;

NSArray *items = [[NSArray alloc] initWithObjects: @"Code Geass", @"Asura Cryin'", @"Voltes V", @"Mazinger Z", @"Daimos", nil];

self.allItems = items;

[items release];

[self.tableView reloadData];

}


在Table View的返回TableView行數的方法中,我們先判斷當前Table View是否是searchDisplayController的查找結果表格還是數據源本來的表格,然后返回對應的行數:

復制代碼 代碼如下:


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

NSInteger rows = 0;

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){

rows = [self.searchResults count];

}else{

rows = [self.allItems count];

}

return rows;

}


在tableView:cellForRowAtIndexPath:方法里,我們需要做同樣的事:

復制代碼 代碼如下:


// Customize the appearance of table view cells.

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

/* Configure the cell. */

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){

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

}else{

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

}

return cell;

}


現在來實現當搜索文本改變時的回調函數。這個方法使用謂詞進行比較,并講匹配結果賦給searchResults數組:

復制代碼 代碼如下:


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];

self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];

}


接下來是UISearchDisplayController的委托方法,負責響應搜索事件:

復制代碼 代碼如下:


#pragma mark - UISearchDisplayController delegate methods

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

return YES;

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {

[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

return YES;

}


運行工程,當你在搜索欄中點擊及輸入文本時,如下圖所示:

201621891348854.jpg (750×716)


UISearchDisplayController 點擊搜索出現黑條問題解決方案
如果點擊按鈕啟動 presentViewController 的時候出現下圖效果:

201621891407773.gif (640×774)

比如說我這里現在代碼式這樣寫的:

復制代碼 代碼如下:

AddFriendViewController *addFriendVC = [[AddFriendViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:addFriendVC];
[self presentViewController:nav animated:YES completion:nil];
[addFriendVC release];
[nav release];


發現問題所在 UINavigationController 的背景顏色是黑色的;

為了解決TableView點擊搜索出現的黑條:

代碼:

復制代碼 代碼如下:

AddFriendViewController *addFriendVC = [[AddFriendViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:addFriendVC];
[nav.view setBackgroundColor:UIColorFromRGB(0xC6C6CB)];
[self presentViewController:nav animated:YES completion:nil];
[addFriendVC release];
[nav release];


改變了Nav的背景色:

復制代碼 代碼如下:


[nav.view setBackgroundColor:UIColorFromRGB(0xC6C6CB)];


效果:

201621891433964.gif (642×554)

通過武林技術頻道小編介紹的iOS之UISearchDisplayController搜索的使用方法,相信大家都有了一定的了解,想要了解更多的技術內容,請繼續關注武林技術頻道吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆城县| 岗巴县| 涿鹿县| 石楼县| 伽师县| 隆安县| 马鞍山市| 额济纳旗| 诸暨市| 凤山县| 谢通门县| 吉安县| 乐清市| 会同县| 镇康县| 汕头市| 广南县| 颍上县| 巴林右旗| 泾阳县| 桂东县| 乐山市| 万载县| 古蔺县| 北海市| 正镶白旗| 会同县| 项城市| 洛川县| 皋兰县| 晋宁县| 娱乐| 延庆县| 湟源县| 江口县| 清流县| 永福县| 南木林县| 太和县| 太湖县| 宁海县|