2016 -03 -07 搜索功能 模糊查询

//gzz0307

-(void)maketitleView11{

    UIView * titleView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ConentViewWidth - 0 , 54)];

    

    searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(10,10, titleView.frame.size.width20, 44)];

    searchTextField.delegate =self;

    

    [searchTextField.layer setMasksToBounds:YES];

    searchTextField.placeholder = @"请输入搜索内容";

    [searchTextField.layer setCornerRadius:6];

    searchTextField.font =[UIFont systemFontOfSize:15];

    

    searchTextField.layer.borderWidth = 1;

    

    CGColorSpaceRef colorO = CGColorSpaceCreateDeviceRGB();

    CGColorRef colorA = CGColorCreate(colorO, (CGFloat[]){0.78,0.78,0.78,1});

    [searchTextField.layer setBorderColor:colorA];

    [titleView addSubview:searchTextField];

    

    UIButton *SearchBtn = [[UIButton alloc]initWithFrame:CGRectMake(titleView.frame.size.width - 45, 20, 23, 23)];

    

    [SearchBtn setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];

    [SearchBtn addTarget:self action:@selector(seacherAction11:) forControlEvents:UIControlEventTouchUpInside];

    [titleView addSubview:SearchBtn];

    [self.view addSubview:titleView];

}

 

//gzzz0307

-(void)seacherAction11:(NSString *)str{

    [self.view endEditing:YES];

    NSLog(@"点击框");

 

    

    if (_isSendmessage) {

        NSLog(@"什么也不做");

    }else{

        NSLog(@"搜索 ///dicMugzz");

        [searchTextField resignFirstResponder];

        

        if (searchTextField.text.length ==0) {

//            ResultDadaArray  = self.SoureDataArray;

            dicMugzz = [[AddressBookManager sharedInstance] NewallContactsBySorted];

 

            [self.tableView reloadData];

            return ;

        }

//        NSMutableArray * seaArray =[[NSMutableArray alloc]initWithArray:self.SoureDataArray];

        NSMutableDictionary *tmpDic=[NSMutableDictionary dictionary];

        tmpDic=[[AddressBookManager sharedInstance] NewallContactsBySorted];

        NSString * searchStr =[KCPinyinHelper pinyinFromChiniseString:searchTextField.text];

        

        if (dicMugzz.count > 0) {

            [dicMugzz removeAllObjects];

            

        }

        [dataDicGzz  removeAllObjects];

        

            NSArray *allkeyss= [[[AddressBookManager sharedInstance] NewallContactsBySorted] allKeys];

            NSLog(@"allkeys===%@",allkeyss);

    

            

            for (int i=0; i<allkeyss.count; i++) {

                NSString *strKey=[allkeyss objectAtIndex:i];

                NSMutableArray *addArr=[tmpDic objectForKey:[NSString stringWithFormat:@"%@",strKey]];

                NSMutableArray *arrMubook=[NSMutableArray array];

 

                for (int j=0; j<addArr.count; j++) {

                    AddressBook  *addbook=[addArr objectAtIndex:j];

                    NSString  *pinStr=[KCPinyinHelper pinyinFromChiniseString:addbook.name];

                    if ([pinStr rangeOfString:searchStr].location == NSNotFound) {

                      NSLog(@"不包含 ===不做处理==");

                    } else {

                      NSLog(@"string存在 ====");

                        [arrMubook addObject:addbook];

                    

                    }

                    

                

                }

                

                

                if (arrMubook.count>0) {

                    [dataDicGzz setObject:arrMubook forKey:strKey];

                }

                

            

            }

        if ([dataDicGzz count]>0) {

            self.localAddressBook=[NSMutableDictionary dictionaryWithDictionary:dataDicGzz];

        }

            [self.tableView reloadData];

 

    }

}

 

 

 

//第二个搜索

-(void)seacherAction{

    NSLog(@"搜索");

    [searchTextField resignFirstResponder];

    

    if (searchTextField.text.length ==0) {

        ResultDadaArray  = self.SoureDataArray;

        [_TV reloadData];

        return ;

    }

    NSMutableArray * seaArray =[[NSMutableArray alloc]initWithArray:self.SoureDataArray];

    NSString * searchStr =[KCPinyinHelper pinyinFromChiniseString:searchTextField.text];

    NSLog(@"seatch  sttrr===%@",searchStr);

    if (ResultDadaArray.count  > 0) {

        [ResultDadaArray removeAllObjects];

 

    }

    [dataArrGzz  removeAllObjects];

    {

        for (int i=0; i<seaArray.count; i++) {

            dataDIcGzz=[NSMutableDictionary dictionaryWithDictionary:[seaArray objectAtIndex:i]];

            NSString  *pinStr=[KCPinyinHelper pinyinFromChiniseString:[NSString stringWithFormat:@"%@",dataDIcGzz[@"realName"]]];

            if ([pinStr containsString:searchStr]) {

                dataArrIndex=i;

                NSLog(@"ii===%zd",i);

                [dataArrGzz addObject:[seaArray objectAtIndex:i]];

//              [_TV reloadData];

            }

        }

        

        

        if(dataArrGzz.count  == 0){

            [self createShowMessage:@"sss"];

            UIImageView *imageV   = [[UIImageView alloc]initWithFrame:CGRectMake((ConentViewWidth-157/2)/2, (ConentViewHeight-181/2)/2-44, 157/2, 181/2)];

            

            imageV.image = [UIImage imageNamed:@"zanwushuju1"];

            

            UILabel *la  =[[UILabel alloc]initWithFrame:CGRectMake((ConentViewWidth-200)/2, (ConentViewHeight-181/2)/2-44+181/2+10, 200, 10)];

            la.text = @"无搜索结果";

            la.textAlignment = NSTextAlignmentCenter;

            la.textColor = [UIColor grayColor];

            [self.view addSubview:la];

            [self.view addSubview:imageV];

            

            [self.view bringSubviewToFront:la];

            [self.view bringSubviewToFront:imageV];

        }

        

        [_TV reloadData];

    }

 

    

    [_TV reloadData];

}

 

 

原文地址:https://www.cnblogs.com/gzz2016/p/5251151.html