搜索框 -- 搜索

//搜索框

    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 58)];

    searchBarView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:searchBarView];

    

    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 12, SCREEN_WIDTH, 34)];

    [searchBarView addSubview:self.searchBar];

    _searchBar.placeholder = @"输入问题或关键字";

    _searchBar.backgroundImage = [WWTolls imageWithColor:[WWTolls colorWithHexString:@"#FFFFFF"]];

    _searchBar.barTintColor = ZDS_BACK_COLOR;

    _searchBar.clipsToBounds = YES;

    

    UIView *searchTextField = [[[_searchBar.subviews firstObject] subviews] lastObject];

    searchTextField.backgroundColor = [WWTolls colorWithHexString:@"#F7F1F1"];

    searchTextField.layer.cornerRadius = 14;

    searchTextField.clipsToBounds = YES;

    [searchTextField setValue:[WWTolls colorWithHexString:@"#BCBCBC"] forKeyPath:@"_placeholderLabel.textColor"];

    

    [_searchBar setImage:[UIImage imageNamed:@"Imported Layers Copy"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

    _searchBar.barStyle = UIBarStyleDefault;

    _searchBar.delegate = self;

    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];

    searchField.textColor = [WWTolls colorWithHexString:@"#535353"];

 

 

#pragma mark UISearchBarDelegate

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar

{

    [self.searchBar resignFirstResponder];

}

 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

{

    NSString *inputStr = searchText;

    

    if([inputStr length] == 0){

        [self searchbarNotext];

        return;

    }

    

    if([WWTolls isHasSensitiveWord:inputStr]){

        [self showAlertMsg:ZDS_HASSENSITIVE yOffset:0];

        return;

    }

    

    NSString *regex = @"[a-zA-Z0-9\u4e00-\u9fa5]+";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    BOOL isMatch = [pred evaluateWithObject:inputStr];

    if(!isMatch){

        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:@"关键字只能由汉字,数字或者英文字母组成" leftButtonTitle:@"" rightButtonTitle:@"确认"];

        [alert show];

    }

    self.searchValue = searchText;

    

    NSLog(@"输入的搜索字符%@",self.searchValue);

}

 

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

    

    if (range.location > 20) {

        return NO;

    }

    

    return YES;

}

 

#pragma mark 点击键盘上搜索按钮

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    mob(@"jy_knowhome_search");

    

    [self.searchBar resignFirstResponder];

    

    [self.buttonView removeFromSuperview];

    [self.scrollView removeFromSuperview];

    [self initTableView];

    

    [self showSearch:self.searchValue];

}

 

- (void)initTableView

{

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 73, SCREEN_WIDTH,SCREEN_HEIGHT - 73 - 64 - 15 - 50)];

    _tableView.backgroundColor = [WWTolls colorWithHexString:@"#F7F1F1"];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    _tableView.showsVerticalScrollIndicator = NO;

    _tableView.scrollsToTop = YES;

    _tableView.sectionFooterHeight = 0;

    _tableView.sectionHeaderHeight = 0;

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:_tableView];

}

 

-(void)searchbarNotext{

    [self.searchBar endEditing:YES];

    

    [self.tableView removeFromSuperview];

    [self setGUI];

}

 

// 查询请求

-(void)showSearch:(NSString*)txt{

    [self showWaitView];

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    [dictionary setObject:txt forKey:@"criteria"];

    [dictionary setObject:@"1" forKey:@"pageNum"];

    [dictionary setObject:@"99" forKey:@"pageSize"];

    

    [WWRequestOperationEngine operationManagerRequest_Post:@"/search/question.do" parameters:dictionary requestOperationBlock:^(NSDictionary *dic) {

        if (!dic[ERRCODE]) {

            [self removeWaitView];

            

            [self.resultsData removeAllObjects];

            NSArray *tempArray = dic[@"questionlist"];

            

            for (NSDictionary *dic in tempArray) {

                MinusAboutKnowModel *model = [MinusAboutKnowModel modelWithDic:dic];

                [self.resultsData addObject:model];

            }

            

            // 将MinusAboutKnowModel数组转化为MinusAboutKnowFrame数组

            NSArray *newFrames = [self knowFramesWithKnow:self.resultsData];

            

            [self.knowFrames removeAllObjects];

            [self.knowFrames addObjectsFromArray:newFrames];

            [self.tableView reloadData];

        }

        

        [self.buttonView removeFromSuperview];

        if (self.knowFrames.count == 0) {

            

            UIView *noResultView = [[UIView alloc] initWithFrame:self.tableView.bounds];

            noResultView.backgroundColor = [WWTolls colorWithHexString:@"#F7F1F1"];

            [self.tableView addSubview:noResultView];

            self.noResultView = noResultView;

            

            UIImageView *tt = [[UIImageView alloc] init];

            tt.image = [UIImage imageNamed:@"kbicon-120"];

            tt.frame = CGRectMake(SCREEN_MIDDLE(60), 85, 60, 60);

            [noResultView addSubview:tt];

            UILabel *ll = [[UILabel alloc] initWithFrame:CGRectMake(0, tt.bottom + 30, SCREEN_WIDTH, 27)];

            ll.font = MyFont(13);

            ll.textColor = [WWTolls colorWithHexString:@"#A7A7A7"];

            ll.textAlignment = NSTextAlignmentCenter;

            ll.text = @"没有搜到相关的内容呢";

            [noResultView addSubview:ll];

            self.noResultView.hidden = NO;

        }else{

            self.noResultView.hidden = YES;

        }

        

        [self removeWaitView];

    }];

}

 

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.knowFrames.count;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    //获得cell

    MinusAboutKnowCell *cell = [MinusAboutKnowCell cellWithTableView:tableView];

    

    // 给cell传递数据模型

    cell.knowFrame = self.knowFrames[indexPath.section];

    

    cell.backgroundColor = [UIColor whiteColor];

    

    return cell;

}

 

 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    MinusAboutKnowFrame *frame = self.knowFrames[indexPath.section];

    return frame.cellHeight;

    

}

 

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

//{

//    return 15;

//}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    MinusAboutKnowFrame *frame = self.knowFrames[indexPath.section];

    MinusAboutKnowModel *model = frame.Knowmodel;

    

    YCProblemDetailViewController *vc = [[YCProblemDetailViewController alloc] init];

    vc.questionid = model.questionid;

    [self.navigationController pushViewController:vc animated:YES];

}

 

- (NSArray *)knowFramesWithKnow:(NSArray *)know{

    // 将know数组转化为WBStatusFrame数组

    NSMutableArray *frames = [NSMutableArray array];

    for (MinusAboutKnowModel *knowmodel in know) {

        MinusAboutKnowFrame *f = [[MinusAboutKnowFrame alloc] init];

        f.Knowmodel = knowmodel;

        [frames addObject:f];

    }

    return [frames copy];

}

 

posted @ 2016-01-11 14:04  十点听风  阅读(227)  评论(0编辑  收藏  举报