模仿手机通讯录,及新浪微博联系人列表查找功能的实现,支持拼音查找,拼音首字母查找等功能。取汉字拼音及拼音首字母是下载网上的源代码,在数据处理中,感觉处理不是太好,但又想不出太好的方法。
原始数据为中英文混合名称的数组,需要取到各名字的首字母分组,并能支持汉字拼音首字母搜索及拼音搜索,主要将原始数据与对应的拼音数据,拼音首字母数据生成字典,使用NSPredicate 过滤器,分别过滤,然后合并到搜索结果数组中
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];NSArray *result1 = [self.allItems filteredArrayUsingPredicate:resultPredicate]; NSArray *result2 = [[tmpDict allKeys] filteredArrayUsingPredicate:resultPredicate];self.searchResults = [[NSMutableArray alloc] initWithArray:result1];for (NSString* str in result2) {NSString* strVal = [tmpDict objectForKey:str];if (![self.searchResults containsObject:strVal]) {[self.searchResults addObject:strVal];} }}
在viewDidLoad进行初始数据准备工作
- (void)viewDidLoad{[super viewDidLoad];// Do any additional setup after loading the view.self.tableView.scrollEnabled = YES;NSArray *items = [[NSArray alloc] initWithObjects:@"大鹏鸟哥",@"Code Geass",@"曹操", @"海贼王专属", @"makuna_mata", @"King3顺", @"sunnysun", @"3M字节", @"李开穷", @"携手漫步",@"四月de蔷薇",@"onepiece海贼王海迷",@"时尚精简语录", @"小S", @"妈唉", nil];tmpDict = [NSMutableDictionary dictionary];NSMutableArray* array = [[NSMutableArray alloc] init];NSMutableArray* itemArray = [[NSMutableArray alloc] init];for (int i = 0; i < [items count]; i++) {NSString *str = [items objectAtIndex:i];char p = [ChineseToPinyin sortSectionTitle:str];NSString* charStr = [[NSString alloc] initWithFormat:@"%c", p];if (![array containsObject:charStr]) {[array addObject:charStr]; }NSString* str2 = [ChineseToPinyin pinyinFirstLetterFromChineseString:str];if (str2) {[tmpDict setObject:str forKey:str2];}NSString* str3 = [ChineseToPinyin pinyinFromChiniseString:str];[tmpDict setObject:str forKey:str3];} //排序数组, 数据量大,效率一般sectionArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];int i = 0;for (NSString *title in sectionArray) {//NSLog(@"%@", title);char ch = [title characterAtIndex:0];indexArray[i++] = [itemArray count];for (NSString *item in items) {char p = [ChineseToPinyin sortSectionTitle:item];if (p == ch) {[itemArray addObject:item];//NSLog(@"%@", item);}}}self.allItems = itemArray;[self.tableView reloadData];}
tableView的 委托,数据源接口实现如下:
-(NSString *)tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)section{ if ([tv isEqual:self.searchDisplayController.searchResultsTableView]){ NSString *str = @"本地搜索结果";return str;}return [sectionArray objectAtIndex:section];}-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tv{if ([tv isEqual:self.searchDisplayController.searchResultsTableView]){ return nil;}return sectionArray;}-(NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section{NSInteger rows = 0; if ([tv isEqual:self.searchDisplayController.searchResultsTableView]){ rows = [self.searchResults count];}else{ NSInteger start = indexArray[section];NSInteger end = indexArray[section+1];if (end == 0) {end = [self.allItems count];}rows = end-start;} return rows;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tv{ if ([tv isEqual:self.searchDisplayController.searchResultsTableView]){ return 1;}return [sectionArray count];}-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"SearchCell"];if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"SearchCell"]; }if ([tv isEqual:self.searchDisplayController.searchResultsTableView]){ cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; }else{NSInteger sec = indexPath.section;NSInteger idx = indexArray[sec];idx += indexPath.row;cell.textLabel.text = [self.allItems objectAtIndex:idx]; } return cell;}
运行结果如下:
如果觉得《自己模仿iphone通信录的搜索联系人的实现》对你有帮助,请点赞、收藏,并留下你的观点哦!