自己模仿iphone通信录的搜索联系人的实现

 模仿手机通讯录,及新浪微博联系人列表查找功能的实现,支持拼音查找,拼音首字母查找等功能。取汉字拼音及拼音首字母是下载网上的源代码,在数据处理中,感觉处理不是太好,但又想不出太好的方法。

 原始数据为 中英文混合名称的数组,需要取到各名字的首字母分组,并能支持汉字拼音首字母搜索及拼音搜索,主要将原始数据与对应的拼音数据,拼音首字母数据生成字典,使用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;
}

运行结果如下:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值