準(zhǔn)備工作:
1燕少、首先引入U(xiǎn)ISearchBarDelegate代理
2、添加一個(gè)數(shù)組,存放搜索結(jié)果
//存放搜索結(jié)果的數(shù)組
@property(nonatomic,strong)NSMutableArray *resultArray;
-(NSMutableArray *)resultArray{
if (!_resultArray) {
_resultArray = [NSMutableArray array];
}
return _resultArray;
}
3熔恢、創(chuàng)建tableview
//創(chuàng)建tableview
-(UItableView *)tableView{
if(!_tableView)
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+64+55, frame.size.width,0)];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.rowHeight = 44;
_tableView.layer.cornerRadius = 5;
_tableView.tableHeaderView = [self headView];
_tableView.tableFooterView = [[UIView alloc]init];
}
4、創(chuàng)建UISearchBar作為tableview的頭部
//UISearchBar作為tableview的頭部
+(UIView *)headView{
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, 100, 44)];
searchBar.keyboardType = UIKeyboardAppearanceDefault;
searchBar.placeholder = @"請輸入搜索關(guān)鍵字";
searchBar.delegate = self;
//底部的顏色
searchBar.barTintColor = KGrayColor;
searchBar.searchBarStyle = UISearchBarStyleDefault;
searchBar.barStyle = UIBarStyleDefault;
return searchBar;
}
5臭笆、實(shí)現(xiàn)UISearchBarDelegate的代理
//在搜索框中修改搜索內(nèi)容時(shí)叙淌,自動(dòng)觸發(fā)下面的方法
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
return YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
NSLog(@"開始輸入搜索內(nèi)容");
searchBar.showsCancelButton = YES;//取消的字體顏色,
[searchBar setShowsCancelButton:YES animated:YES];
//改變?nèi)∠奈谋? for(UIView *view in [[[searchBar subviews] objectAtIndex:0] subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *cancel =(UIButton *)view;
[cancel setTitle:@"取消" forState:UIControlStateNormal];
[cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
NSLog(@"輸入搜索內(nèi)容完畢");
}
--->搜框中輸入關(guān)鍵字進(jìn)行搜索
//搜框中輸入關(guān)鍵字的事件響應(yīng)
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"輸入的關(guān)鍵字是---%@---%lu",searchText,(unsigned long)searchText.length);
//需要事先情況存放搜索結(jié)果的數(shù)組
[self.resultArray removeAllObjects];
//加個(gè)多線程愁铺,否則數(shù)量量大的時(shí)候鹰霍,有明顯的卡頓現(xiàn)象
//這里最好放在數(shù)據(jù)庫里面再進(jìn)行搜索,效率會(huì)更快一些
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_async(globalQueue, ^{
if (searchText!=nil && searchText.length>0) {
//遍歷需要搜索的所有內(nèi)容茵乱,其中self.dataArray為存放總數(shù)據(jù)的數(shù)組
for (DataModel *model in self.dataArray) {
NSString *tempStr = model.content;
//----------->把所有的搜索結(jié)果轉(zhuǎn)成成拼音
NSString *pinyin = [self transformToPinyin:tempStr];
NSLog(@"pinyin--%@",pinyin);
if ([pinyin rangeOfString:searchText options:NSCaseInsensitiveSearch].length >0 ) {
//把搜索結(jié)果存放self.resultArray數(shù)組
[self.resultArray addObject:model];
}
}
}else{
self.resultArray = [NSMutableArray arrayWithArray:self.dataArray];
}
//回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
});
}
//取消的響應(yīng)事件
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
NSLog(@"取消搜索");
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}
//鍵盤上搜索事件的響應(yīng)
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"搜索");
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}
!!!獲取漢字轉(zhuǎn)成拼音字符串 通訊錄模糊搜索 支持拼音檢索 首字母 全拼 漢字 搜索
- (NSString *)transformToPinyin:(NSString *)aString
{
//轉(zhuǎn)成了可變字符串
NSMutableString *str = [NSMutableString stringWithString:aString];
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
//再轉(zhuǎn)換為不帶聲調(diào)的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
NSMutableString *allString = [NSMutableString new];
int count = 0;
for (int i = 0; i < pinyinArray.count; i++)
{
for(int i = 0; i < pinyinArray.count;i++)
{
if (i == count) {
[allString appendString:@"#"];//區(qū)分第幾個(gè)字母
}
[allString appendFormat:@"%@",pinyinArray[i]];
}
[allString appendString:@","];
count ++;
}
NSMutableString *initialStr = [NSMutableString new];//拼音首字母
for (NSString *s in pinyinArray)
{
if (s.length > 0)
{
[initialStr appendString: [s substringToIndex:1]];
}
}
[allString appendFormat:@"#%@",initialStr];
[allString appendFormat:@",#%@",aString];
return allString;
}
tableView展示搜索內(nèi)容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//把符合搜索條件的內(nèi)容展示出來
JXSChannelModel *model = self.resultArray[indexPath.row];
cell.textLabel.text = model.ChannelName;
}
Demo下載地址:https://github.com/zhuchenglong/MySearchBarDemo