自己模仿iphone通信录的搜索联系人的实现
2012-07-25 16:42
375 查看
模仿手机通讯录,及新浪微博联系人列表查找功能的实现,支持拼音查找,拼音首字母查找等功能。取汉字拼音及拼音首字母是下载网上的源代码,在数据处理中,感觉处理不是太好,但又想不出太好的方法。
原始数据为 中英文混合名称的数组,需要取到各名字的首字母分组,并能支持汉字拼音首字母搜索及拼音搜索,主要将原始数据与对应的拼音数据,拼音首字母数据生成字典,使用NSPredicate 过滤器,分别过滤,然后合并到搜索结果数组中
运行结果如下:
原始数据为 中英文混合名称的数组,需要取到各名字的首字母分组,并能支持汉字拼音首字母搜索及拼音搜索,主要将原始数据与对应的拼音数据,拼音首字母数据生成字典,使用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字节", @"李开穷", @"携手漫步2012",@"四月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:UITableViewCellStyleDefault reuseIdentifier:@"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; }
运行结果如下:
相关文章推荐
- [HOW TO]怎么在iPhone程序中实现可多选可搜索按字母排序的联系人选择器
- Android通讯录开发之通讯录联系人搜索功能最新实现
- [.NET] 自己实现任务池(模仿线程池)
- 【AJAX】AJAX实现搜索信息自己主动推荐并补全
- java RMI一个例子以及模仿RMI的原理实现一个自己的RMI
- android listview 圆角的实现方案,模仿Iphone的UITableView
- 利用jQuery和css实现的模仿百度搜索列表页面的分页的足迹效果
- iphone 通讯录 搜索 效果实现
- 原生js实现拖动图片左右弹性切换(模仿iPhone弹性拖拽的功能)
- java Swing模仿百度搜索功能的实现
- 实现自己的音乐搜索软件(一)
- android 拼音搜索联系人分析与实现(支持多音字,支持T9搜索,支持Qwerty搜索)
- [转]怎么样把百度搜索引入自己的网站JS实现(附源代码)
- Android实现列表仿联系人快速查找和关键字搜索
- android 如何实现联系人搜索(动态刷新view)
- 实现在自己的程序中点击一个按钮,然后弹出系统自带的联系人
- Android 联系人列表界面(仿iphone、A~Z字母排列、过滤搜索)
- 自己做的 code_kk 的安卓客户端,数据是抓包来的,实现了每日开源项目的展示,和搜索功能,收藏功能是存在了本地数据库。
- 怎么样把百度搜索引入自己的网站JS实现(附源代码)
- 实现自己的(模仿jquery)toggle函数