//
// ViewController.m
// locationSerBar
//
// Created by 添锦 on 16/3/3.
// Copyright © 2016年 添锦. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_searchBar.delegate = self;
_dataArray = [[NSArray alloc]initWithObjects:@"好",@"很好",@"很好",@"手动阀",@"问", nil];
_dataSource = [NSMutableArray arrayWithArray:_dataArray];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText != nil && searchText.length > 0) {
_dataSource = [NSMutableArray array];
for (NSString *tempStr in _dataArray) {
NSMutableString *ms = [[NSMutableString alloc] initWithString:searchText];
NSMutableString *ts = [[NSMutableString alloc] initWithString:tempStr];
//进行转换
CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)ts, 0, kCFStringTransformMandarinLatin, NO);
/**
**这两行可以去除音调
*/
CFStringTransform((__bridge CFMutableStringRef)ms, NULL, kCFStringTransformStripDiacritics, NO);
CFStringTransform((__bridge CFMutableStringRef)ts, NULL, kCFStringTransformStripDiacritics, NO);
NSLog(@"去掉声母号 %@",ms);
if ([ts rangeOfString:ms options:NSCaseInsensitiveSearch].length >0 ) {
[_dataSource addObject:tempStr];
}
[_tableView reloadData];
}
}else{
_dataSource = [NSMutableArray arrayWithArray:_dataArray];
[_tableView reloadData];
}
}
//-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// [self searchBar:self.searchBar textDidChange:nil];
//}
//
//- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
//{
// [self searchBar:self.searchBar textDidChange:nil];
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_dataSource count] > 0 ?[_dataSource count]:0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if([_dataSource count]!= 0 && ![_dataSource isKindOfClass:[NSNull class]] && _dataSource !=nil){
cell.textLabel.text = _dataSource[indexPath.row];
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end