最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何實(shí)現(xiàn)IOS_SearchBar搜索欄及關(guān)鍵字高亮

 更新時(shí)間:2016年08月11日 14:50:53   作者:嘴角微寒  
本文通過(guò)實(shí)例代碼演示如何實(shí)現(xiàn)IOS搜索欄及搜索關(guān)鍵字高亮,效果很棒,小編覺(jué)得對(duì)大家的學(xué)習(xí)會(huì)很有幫助,現(xiàn)在分享給大家,有需要的可以參考學(xué)習(xí)。

搜索框的效果演示:

這個(gè)就是所謂的搜索框了,那么接下來(lái)我們看看如何使用代碼來(lái)實(shí)現(xiàn)這個(gè)功能.

我所使用的數(shù)據(jù)是英雄聯(lián)盟的英雄名單,是一個(gè)JSON數(shù)據(jù)的txt文件, JSON數(shù)據(jù)的處理代碼如下所示:

//獲取文件的路徑path
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"txt"];
//將路徑下的文件轉(zhuǎn)換成NSData數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfFile:path];
//將得到的NSdata數(shù)據(jù)進(jìn)行JSON解析并返回一個(gè)結(jié)果數(shù)組result
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

我們?cè)賮?lái)看數(shù)據(jù)的層級(jí)關(guān)系:


這里解釋下,這個(gè)層級(jí)關(guān)系是通過(guò)在線代碼格式化網(wǎng)頁(yè)得到的,我們上一步所做的數(shù)據(jù)處理就是將原始數(shù)據(jù)進(jìn)行處理,得到一個(gè)結(jié)果數(shù)組,他的層級(jí)關(guān)系和格式化后一樣,這樣就可以根據(jù)格式化網(wǎng)頁(yè)上的層級(jí)關(guān)系來(lái)進(jìn)一步處理數(shù)據(jù),將需要的內(nèi)容放入數(shù)組或者字典(當(dāng)然也可以直接打印result來(lái)看層級(jí)關(guān)系,看個(gè)人習(xí)慣).

那么我們所需要的內(nèi)容就是字典中nick所對(duì)應(yīng)的值,通過(guò)遍歷將其取出來(lái)放入數(shù)組中,這里將這個(gè)數(shù)組定義為屬性,在其他方法里會(huì)用到.

// 將搜索范圍的內(nèi)容放入數(shù)組
for (NSDictionary *diction in result) {
  [self.arrOfSeachBoxes addObject:diction[@"nick"]];
 }

接下來(lái)我們創(chuàng)建一個(gè)UITableView用來(lái)顯示數(shù)據(jù),搜索條需要用到的類是UISearchController,先看看如何創(chuàng)建:

系統(tǒng)的注釋說(shuō)的很清楚,如果想要在當(dāng)前頁(yè)顯示搜索結(jié)果,這個(gè)方法的參數(shù)填nil即可,為了方便起見,聲明一個(gè)UISearchController的屬性

@property (nonatomic, retain) UISearchController *searchController;

接下來(lái)是創(chuàng)建

// nil表示在當(dāng)前頁(yè)面顯示搜索結(jié)果
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

UISearchController頭文件中被放在非??壳暗奈恢玫氖且粋€(gè)屬性


根據(jù)字面意思我們可以猜到這跟搜索結(jié)果的更新有關(guān),就跟tableViewreloadData一個(gè)意思.那么很明顯,我們得簽協(xié)議<UISearchResultsUpdating>,這個(gè)協(xié)議中只有一個(gè)必須要實(shí)現(xiàn)的方法.

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

頭文件如下圖所示:

---------這里是美麗的分割線---------

上面已經(jīng)把所有關(guān)于搜索條的類和方法羅列了一下,下面來(lái)捋一捋

所有定義的屬性如下所示:

NS_ASSUME_NONNULL_BEGIN
@interface ViewController () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating>
@property (nonatomic, retain) NSMutableArray *arrOfSeachBoxes;/**< 搜索范圍 */
@property (nonatomic, retain) NSMutableArray *arrOfSeachResults;/**< 搜索結(jié)果 */
@property (nonatomic, retain) UISearchController *searchController;
@property (nonatomic, retain) UITableView *tableView;
@end
NS_ASSUME_NONNULL_END

數(shù)據(jù)處理相關(guān)代碼如下:

// 解析數(shù)據(jù)
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
self.arrOfSeachBoxes = [NSMutableArray array];
// 將搜索范圍的內(nèi)容放入數(shù)組
for (NSDictionary *dic in result) {
 [self.arrOfSeachBoxes addObject:dic[@"nick"]];
}

和UISearchController的創(chuàng)建相關(guān)代碼如下:

// 創(chuàng)建
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

//searchBar的frame
self.searchController.searchBar.frame = CGRectMake(0, 44, 0, 44);

// 是否需要在輸入搜索內(nèi)容時(shí)變暗
self.searchController.dimsBackgroundDuringPresentation = false;

self.searchController.searchBar.showsCancelButton = YES;/**< 取消按鈕 */

self.searchController.searchResultsUpdater = self;/**< 顯示搜索結(jié)果的VC */

self.searchController.active = YES;/**< 搜索結(jié)果顯示 */

和tableView相關(guān)的代碼如下:

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

[self.view addSubview:self.tableView];

self.tableView.delegate = self;

self.tableView.dataSource = self;

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

//將SearchBar放在tableView的頭部視圖 
self.tableView.tableHeaderView = self.searchController.searchBar;

UISearchResultsUpdating協(xié)議方法代碼如下:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

//初始化存儲(chǔ)搜索結(jié)果的數(shù)組
self.arrOfSeachResults = [NSMutableArray array];

// 獲取關(guān)鍵字
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchController.searchBar.text];

// 用關(guān)鍵字過(guò)濾數(shù)組中的內(nèi)容, 將過(guò)濾后的內(nèi)容放入結(jié)果數(shù)組
self.arrOfSeachResults = [[self.arrOfSeachBoxes filteredArrayUsingPredicate:predicate] mutableCopy];

// 完成數(shù)據(jù)的過(guò)濾和存儲(chǔ)后刷新tableView.
[self.tableView reloadData];
}

tableView的DataSource

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

// 顯示搜索結(jié)果時(shí)
if (self.searchController.active) {

 //以搜索結(jié)果的個(gè)數(shù)返回行數(shù)
 return self.arrOfSeachResults.count;
}
 //沒(méi)有搜索時(shí)顯示所有數(shù)據(jù)
 return self.arrOfSeachBoxes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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

// 顯示搜索結(jié)果時(shí)
if (self.searchController.active) {

// 原始搜索結(jié)果字符串.
NSString *originResult = self.arrOfSeachResults[indexPath.row];

// 獲取關(guān)鍵字的位置
NSRange range = [originResult rangeOfString:self.searchController.searchBar.text];

// 轉(zhuǎn)換成可以操作的字符串類型.
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:originResult];

// 添加屬性(粗體)
[attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range];

// 關(guān)鍵字高亮
[attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];

// 將帶屬性的字符串添加到cell.textLabel上.
[cell.textLabel setAttributedText:attribute];

cell.textLabel.text = self.arrOfSeachResults[indexPath.row];

 } else {

 cell.textLabel.text = self.arrOfSeachBoxes[indexPath.row];

  }

 return cell;
}

總結(jié)

以上就是如何實(shí)現(xiàn)IOS搜索欄及搜索關(guān)鍵字高亮的全部?jī)?nèi)容,感興趣的同學(xué)可以自己動(dòng)手操作實(shí)現(xiàn)下,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

桦甸市| 贵定县| 东宁县| 碌曲县| 建昌县| 亳州市| 滨州市| 荥阳市| 沧州市| 四平市| 武宁县| 诏安县| 牟定县| 乌鲁木齐市| 齐河县| 抚顺市| 南漳县| 普宁市| 阜康市| 洛宁县| 鄂托克前旗| 蓝山县| 麻阳| 嘉善县| 陇南市| 高清| 常州市| 大竹县| 衡东县| 江门市| 广安市| 三河市| 体育| 高阳县| 措美县| 陈巴尔虎旗| 布拖县| 建昌县| 乌什县| 临江市| 兴义市|