标签:
时事检索功能还是挺好实现的,先看效果图。
tableView索引实现,还是先看效果图
下面是实际代码,注释基本很详细
CityListViewController.h文件里面,传来一个数组,是城市解析出来后的的数据
// // CityListViewController.h // WisdomShope // // Created by mac on 15/12/26. // Copyright (c) 2015年 ZY. All rights reserved. // #import <UIKit/UIKit.h> @interface CityListViewController : UIViewController @property (nonatomic, strong) NSArray *cityListArray; @end
CityListViewController.m文件
// // CityListViewController.m // WisdomShope // // Created by mac on 15/12/26. // Copyright (c) 2015年 ZY. All rights reserved. // #import "CityListViewController.h" #import "CitiesModel.h" @interface CityListViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating> { //索引数组 NSArray *sectionCityArray; //热门城市 NSMutableArray *fistArray; //当前城市数组 NSArray *currentArray; //所有城市的字典 NSMutableDictionary *citiseDic; UITableView *cityTableView; } // 根据searchController搜索的城市 盛放检索结果的数组 @property(strong, nonatomic) NSMutableArray *filteredCities; // 所有城市数组,单独取出,以用于输入汉字检索 @property(strong, nonatomic) NSMutableArray *allCities; //所有城市的拼音数组 @property(strong, nonatomic) NSMutableArray *PYCities; @end @implementation CityListViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { [self creatSubViews]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"城市列表"; } - (void)creatSubViews{ // 创建SearchBar UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 64, kScreenW - 10, 50)]; searchBar.barTintColor = [UIColor whiteColor]; searchBar.placeholder = @"请输入城市中文名或拼音"; searchBar.delegate = self; //searchBar是否显示取消按钮 searchBar.showsCancelButton = YES; searchBar.layer.cornerRadius = 10; searchBar.clipsToBounds = YES; searchBar.layer.borderWidth = 2; searchBar.layer.borderColor = [[UIColor grayColor] CGColor]; [self.view addSubview:searchBar]; cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, kScreenW, kScreenH - 64 - 50 - 49) style:UITableViewStylePlain]; cityTableView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:cityTableView]; cityTableView.delegate = self; cityTableView.dataSource = self; //索引字符的颜色 cityTableView.sectionIndexColor = [UIColor cyanColor]; cityTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //设置索引栏点击时的颜色 cityTableView.sectionIndexTrackingBackgroundColor = [UIColor lightGrayColor]; cityTableView.sectionIndexBackgroundColor = [UIColor whiteColor]; } - (void)setCityListArray:(NSArray *)cityListArray{ _cityListArray = cityListArray; _allCities = [[NSMutableArray alloc] init]; _PYCities = [[NSMutableArray alloc] init]; //索引数组,也是分组的头视图的内容 sectionCityArray = @[@"当前",@"热门",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"W",@"X",@"Y",@"Z"]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *cityName = [defaults objectForKey:@"cityName"]; currentArray = @[cityName]; //热门城市数组 fistArray = [[NSMutableArray alloc] init]; citiseDic = [[NSMutableDictionary alloc] init]; for (CitiesModel *model in _cityListArray) { NSMutableArray *cityArr = [citiseDic objectForKey:model.charindex]; [_allCities addObject:model.name]; //汉字转化为拼音 后面的方法是自己封装的汉字转拼音的方法,在最后会附上转化的代码 NSString *resuString = [ChineseToEn chineseToEnglish:model.name]; NSString *upStr = [resuString uppercaseString]; [_PYCities addObject:upStr]; if (!cityArr) { NSMutableArray *newArr = [[NSMutableArray alloc] init]; [newArr addObject:model]; [citiseDic setValue:newArr forKey:model.charindex]; }else { [cityArr addObject:model]; } if ([model.level isEqualToString:@"1"]) { [fistArray addObject:model]; [citiseDic setObject:fistArray forKey:@"热门城市"]; } } [cityTableView reloadData]; } #pragma mark tableView Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if(_filteredCities.count > 0){ return 1; } return sectionCityArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(_filteredCities.count > 0){ return _filteredCities.count; } if (section == 0) { return currentArray.count; }else if (section == 1) { return fistArray.count; } NSString *name = sectionCityArray[section]; NSArray *array = [citiseDic objectForKey:name]; return array.count; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20; } //返回组的头视图 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if(_filteredCities.count > 0){ return @"符合搜索类型的城市"; } return sectionCityArray[section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentifier = @"cityCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]; } cell.backgroundColor = [UIColor whiteColor]; if(_filteredCities.count > 0){ cell.textLabel.text = _filteredCities[indexPath.row]; return cell; } if (indexPath.section == 0) { cell.textLabel.text = currentArray.firstObject; }else if (indexPath.section == 1) { CitiesModel *model = fistArray[indexPath.row]; cell.textLabel.text = model.name; }else{ NSString *name = sectionCityArray[indexPath.section]; NSArray *array = [citiseDic objectForKey:name]; CitiesModel *model = array[indexPath.row]; cell.textLabel.text = model.name; } return cell; } #pragma mark tableViewCell的点击方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [[NSNotificationCenter defaultCenter] postNotificationName:@"cityName" object:nil userInfo:@{@"city":cell.textLabel.text}]; //城市名,放到属性列表中 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:cell.textLabel.text forKey:@"cityName"]; [self.navigationController popViewControllerAnimated:YES]; } //添加索引 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ if(_filteredCities.count > 0){ return @[@""]; } return sectionCityArray; } #pragma mark 索引的点击事件 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ [self showAlertViewShowTitle:title]; return [sectionCityArray indexOfObject:title]; } #pragma mark 索引点击时提示点击的索引位置 - (void)showAlertViewShowTitle:(NSString *)title{ //创建label 用于显示点击索引栏时,点击的索引值 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kScreenW/2-25, kScreenH/2 - 25, 50, 50)]; label.backgroundColor = [UIColor grayColor]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:35]; if ([title isEqualToString:@"当前"]) { label.font = [UIFont systemFontOfSize:17]; } if ([title isEqualToString:@"热门"]) { label.font = [UIFont systemFontOfSize:17]; } label.text = title; label.tag = 20152808; [self.view addSubview:label]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [label removeFromSuperview]; }); } #pragma mark searchBar delegate - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self.filteredCities removeAllObjects]; NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText]; self.filteredCities = [[self.allCities filteredArrayUsingPredicate:searchPredicate] mutableCopy]; //根据拼音进行检索 if (_filteredCities.count == 0) { NSString *upSeT = [searchText uppercaseString]; for(int i = 0;i < _PYCities.count;i++){ NSString *cityN = _PYCities[i]; if ([cityN rangeOfString:upSeT].location != NSNotFound) { [_filteredCities addObject:_allCities[i]]; } } } dispatch_async(dispatch_get_main_queue(), ^{ [cityTableView reloadData]; }); } //点击取消按钮调用的方法 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [searchBar resignFirstResponder]; } - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ } - (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"city" object:nil]; } @end
拼音转汉字的方法
+ (NSString *)chineseToEnglish:(NSString *)str{ NSString *resuStr; if (str.length > 0) { NSMutableString *mutableStr = [[NSMutableString alloc] initWithString:str]; if (CFStringTransform((__bridge CFMutableStringRef)mutableStr, 0, kCFStringTransformMandarinLatin, NO)) { } if (CFStringTransform((__bridge CFMutableStringRef)mutableStr, 0, kCFStringTransformStripDiacritics, NO)) { resuStr = mutableStr; } } return resuStr; }
使用UISearchBar+UITableView进行的时事检索功能的实现,及TabelView索引的实现
标签:
原文地址:http://www.cnblogs.com/zxh-iOS/p/5094665.html