// // searchController.m // ThePaperHD // // Created by YoungLee on 15/4/14. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicSearchController.h" #import "MLNavigationController.h" #import "topicSearchCell.h" #import "KGModal.h" @interface topicSearchController () { NSString *searchStr; } @property(nonatomic, strong)UIButton *closeBtn; @property(nonatomic, strong)UILabel *titleLabel; @property(nonatomic, strong)UIImageView *searchIcon; @property(nonatomic, strong)UITextField *searchContent; @property(nonatomic, strong)UIButton *searchBtn; @property(nonatomic, strong)UIView *line; @property(nonatomic, strong)UITableView *searchList; @property(nonatomic, strong)NSArray *dataList; @property(nonatomic, strong)NSMutableArray *heightArray; @property(nonatomic, strong)TPTableViewController *tableviewvc; @property(nonatomic, strong)UILabel *noDataView; @property(nonatomic, strong)NSString *nextUrl; @property(nonatomic, strong)NSString *hotKey; @end @implementation topicSearchController @synthesize isNeedHold; @synthesize dataList= _dataList; @synthesize heightArray; @synthesize hotKey; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor clearColor]; self.view.frame = CGRectMake(0, 0, settingPopSize.width, settingPopSize.height); [MobClick event:@"48"]; [self.view addSubview:self.closeBtn]; [self.view addSubview:self.titleLabel]; self.closeBtn.frame = CGRectMake(0, 0, 50, 80); [self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)]; self.titleLabel.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 80); self.keyboardView.frame = CGRectMake(0, 80, searchPopSize.width, searchPopSize.height-80); self.keyboardView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [self addChildViewController:self.tableviewvc]; [self.keyboardView addSubview:self.searchIcon]; [self.keyboardView addSubview:self.searchContent]; [self.keyboardView addSubview:self.searchBtn]; [self.keyboardView addSubview:self.line]; // [self.keyboardView addSubview:self.searchList]; [self.keyboardView addSubview:self.noDataView]; [self.view addSubview:self.tableviewvc.view]; [self.tableviewvc configCanRefresh:NO canLoad:YES]; self.tableviewvc.view.hidden = YES; self.tableviewvc.view.backgroundColor = [UIColor clearColor]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self enablePopBackTap]; self.isNeedHold = NO; [self.searchContent becomeFirstResponder]; [KGModal sharedInstance].tapOutsideToDismiss = YES;//搜索:搜索窗口没有自动关闭,在用户点击窗口以外的区域时(bug:3895) __weak typeof(self) Self = self; [[KGModal sharedInstance] setTapHandler:^{ [Self closeKeyBoard]; }]; // [Remote doJsonActionWithBlock:0 requestUrl:getHotKeyURL parameter:nil withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { // if (success) { // NSArray *array = responseData[@"keys"]; // hotKey = array[0]; // NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"大家都在搜:%@",array[0]] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:LINECOLOR]}]; // self.searchContent.attributedPlaceholder = atrString; // }else{ // self.searchContent.placeholder = @""; // ShowTextMessage(message); // } // }]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].msgMark]; [dic setValue:@(0) forKey:@"searchMark"]; [TPUserDefault instance].msgMark = dic; // if ([self.delegate respondsToSelector:@selector(hasAppearSearchCenter)]) { // [self.delegate hasAppearSearchCenter]; // } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)beginToPullLoad{ if (isBlankString(self.tableviewvc.nextUrl)) { TPLOG(@"数据有问题,无法获取新数据"); [self.tableviewvc loadSuccess]; return; } // kCFStringEncodingGB_18030_2000 // NSString *url = [self.tableviewvc.nextUrl stringByAddingPercentEscapesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)]; NSString *url = [self.tableviewvc.nextUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // NSString *dataString = [[NSString alloc] initWithData:[self.tableviewvc.nextUrl dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding]; [Remote doJsonAction:2 requestUrl:url parameter:nil delegate:self]; } #pragma mark - remote handler - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 2) { NSMutableArray *oldSearchList = [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *newSearchList = [NSMutableArray array]; NSArray *list = responsData[@"searchList"]; [list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { // 【倒退】ipad进入问吧页,点击搜索,随便输入一个关键字搜索后,上拉刷新加载必现闪退(bug:4908) // listContObjectVO *listBo = setJsonDicToDataModel(obj, [listContObjectVO class]); TopicInfoBO *listBo = setJsonDicToDataModel(obj, [TopicInfoBO class]); [newSearchList addObject:listBo]; }]; [oldSearchList addObjectsFromArray:newSearchList]; self.dataList = oldSearchList; [self.tableviewvc loadSuccess]; NSString *url = responsData[@"nextUrl"]; self.tableviewvc.nextUrl = url; } } - (void)keyboardWillShow:(NSNotification *)aNotification { } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code { ShowTextMessage(message); [self.tableviewvc loadSuccess]; } - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (UIButton*)closeBtn { if (!_closeBtn) { _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_closeBtn setImage:Image(@"login/popUpCloseBtn.png") forState:UIControlStateNormal]; [_closeBtn setImage:Image(@"login/popUpCloseBtn_s.png") forState:UIControlStateHighlighted]; [_closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside]; } return _closeBtn; } - (UILabel*)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _titleLabel.backgroundColor = [UIColor clearColor]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.text = @"话题搜索";//【需求】搜索:增加话题的搜索功能(bug:4314) _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.font = appFont(30, NO); } return _titleLabel; } -(UIImageView *)searchIcon{ if (!_searchIcon) { _searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 20, 20)]; _searchIcon.image = Image(@"setting/setting_search.png"); } return _searchIcon; } -(UITextField *)searchContent{ if (!_searchContent) { _searchContent = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.searchIcon.frame)+10, 15, self.view.frame.size.width-55-CGRectGetMaxX(self.searchIcon.frame), 20)]; _searchContent.textColor = [UIColor colorWithHexString:TextBlack]; _searchContent.textAlignment = NSTextAlignmentLeft; _searchContent.keyboardAppearance = UIKeyboardAppearanceDefault; if ([[TPUserDefault instance].isNightMode intValue] > 0) { _searchContent.keyboardAppearance = UIKeyboardAppearanceDark; } _searchContent.keyboardType = UIKeyboardTypeDefault; _searchContent.clearButtonMode = UITextFieldViewModeWhileEditing; _searchContent.font = appFont(18, NO); _searchContent.delegate = self; //话题:夜间模式下,搜索弹出窗口中没有默认提示信息“话题搜索”字样,且点击搜索后,无任何提示(bug:4453) UIColor *color = [UIColor colorWithHexString:LINECOLOR]; _searchContent.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"话题搜索" attributes:@{NSForegroundColorAttributeName: color}]; // [_searchContent addTarget:self action:@selector(editChange:) forControlEvents:UIControlEventEditingChanged];//搜索: 搜索输入框中输入超过20个字后,仅显示20个字,后面的为省略号(bug:4298) [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(editChange:) name:UITextFieldTextDidChangeNotification object:_searchContent]; } return _searchContent; } -(UIButton *)searchBtn{ if (!_searchBtn) { _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _searchBtn.frame = CGRectMake(CGRectGetMaxX(self.searchContent.frame), 15, 40, 20); _searchBtn.titleLabel.font = appFont(18, NO); [_searchBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal]; [_searchBtn setTitleColor:[UIColor colorWithHexString:LINECOLOR] forState:UIControlStateHighlighted]; [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal]; [_searchBtn addTarget:self action:@selector(searchBtnSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _searchBtn; } -(UIView *)line{ if (!_line) { _line = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.searchIcon.frame)+5, self.view.frame.size.width -70, 1)]; _line.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _line; } - (TPTableViewController*)tableviewvc { if (!_tableviewvc) { _tableviewvc = TPTableViewController.new; _tableviewvc.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _tableviewvc.view.frame = CGRectMake(0, 130, self.view.frame.size.width,searchPopSize.height-130); _tableviewvc.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableviewvc.view.clipsToBounds = YES; _tableviewvc.tableView.delegate = self; _tableviewvc.tableView.dataSource = self; _tableviewvc.tableDelegate = self; } return _tableviewvc; } -(UILabel *)noDataView{ if (!_noDataView) { _noDataView = [[UILabel alloc]initWithFrame:CGRectMake(0,50, searchPopSize.width, CGRectGetHeight(self.keyboardView.bounds)-50)]; _noDataView.hidden = YES; _noDataView.textAlignment = NSTextAlignmentCenter; _noDataView.font = appFont(20, NO); _noDataView.textColor = [UIColor colorWithHexString:TextBlack]; _noDataView.text = @"暂无相关话题"; _noDataView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _noDataView; } #pragma mark - event - (void)closeEvent:(UIButton*)btn { [self dismissControllerAnimated:YES completion:nil]; } -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (toBeString.length > 20 && range.length!=1){ textField.text = [toBeString substringToIndex:20]; return NO; } return YES; } -(void) editChange:(NSNotification*) obj{ UITextField *textField = (UITextField *)obj.object; NSString *toBeString = textField.text; NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式 if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写 UITextRange *selectedRange = [textField markedTextRange]; //获取高亮部分 UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0]; // 没有高亮选择的字,则对已输入的文字进行字数统计和限制 if (!position) { if (toBeString.length > 20) { textField.text = [toBeString substringToIndex:20]; } } // 有高亮选择的字符串,则暂不对文字进行统计和限制 else{ } } // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况 else{ if (toBeString.length > 20) { textField.text = [toBeString substringToIndex:20]; } } } -(void) searchBtnSelector:(UIButton *)btn{ NSDictionary *dict = [NSDictionary dictionary]; if (isBlankString(self.searchContent.text)) {//话题:夜间模式下,搜索弹出窗口中没有默认提示信息“话题搜索”字样,且点击搜索后,无任何提示(bug:4453) [self.searchContent resignFirstResponder]; ShowTextMessage(@"搜索关键词不能为空!"); return; }else{ NSString *content = self.searchContent.text; NSArray *contentList = [content componentsSeparatedByRegex:@"\\s+"]; //去除空格组成标准关键词(bug:3881) searchStr = @""; if (contentList.count > 0) { [contentList enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) { if (idx != contentList.count-1) { searchStr = [searchStr stringByAppendingString:[NSString stringWithFormat:@"%@ ",obj]]; }else { searchStr = [searchStr stringByAppendingString:obj]; } }]; }else { searchStr = content; } dict = @{@"k":searchStr}; } [self remoteAskForSearchList:dict]; } - (void)remoteAskForSearchList:(NSDictionary*)dict { __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:1 requestUrl:topicSearchURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { [Self closeKeyBoard]; self.tableviewvc.view.hidden = NO; if (success) { NSMutableArray *list = responseData[@"searchList"]; NSMutableArray *searchList = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { TopicInfoBO *listBo = setJsonDicToDataModel(obj, [TopicInfoBO class]); [searchList addObject:listBo]; }]; Self.dataList = searchList; NSString *url = responseData[@"nextUrl"]; Self.tableviewvc.nextUrl = url; }else { ShowTextMessage(message); } }]; } //- (void)remoteAskForSearchList:(NSDictionary*)dict { // __weak typeof(self) Self = self; // [Remote doJsonActionWithBlock:1 requestUrl:searchURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { // [Self closeKeyBoard]; // self.tableviewvc.view.hidden = NO; // if (success) { // NSMutableArray *list = responseData[@"searchList"]; // NSMutableArray *searchList = [NSMutableArray array]; // [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // listContObjectVO *listBo = setJsonDicToDataModel(obj, [listContObjectVO class]); // [searchList addObject:listBo]; // }]; // Self.dataList = searchList; // NSString *url = responseData[@"nextUrl"]; // Self.tableviewvc.nextUrl = url; // }else { // ShowTextMessage(message); // } // }]; //} -(void)setDataList:(NSArray *)list{ _dataList = list; heightArray = [NSMutableArray array]; if (_dataList.count > 0) { self.tableviewvc.view.hidden = NO; self.noDataView.hidden = YES; for (int i =0 ; i < self.dataList.count; i++) { TopicInfoBO *topic = self.dataList[i]; CGFloat titleHeight = returnTextHeightWithRTLabel(topic.title, searchPopSize.width-30, appFont(17, NO), 8); // CGFloat titleHeight = heightForString(topic.title,appFont(17, NO),searchPopSize.width-30, NSLineBreakByWordWrapping); [heightArray addObject:@(titleHeight+36)]; } [self.tableviewvc.tableView reloadData]; }else{ self.tableviewvc.view.hidden = YES; self.noDataView.hidden = NO; } } #pragma mark - uitableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [heightArray[indexPath.row] floatValue]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *searchCellID = @"topicSearchCell"; topicSearchCell *cell = (topicSearchCell*)[tableView dequeueReusableCellWithIdentifier:searchCellID]; if (nil == cell) { cell = [[topicSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:searchCellID]; } [cell setData:self.dataList[indexPath.row] key:self.searchContent.text]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; TopicInfoBO *topic = self.dataList[indexPath.row]; self.isNeedHold = YES; if ([self.delegate respondsToSelector:@selector(searchToFilter:)]) { [self.delegate searchToFilter:topic]; } } -(void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:self.searchContent]; } //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // [tableView deselectRowAtIndexPath:indexPath animated:YES]; // id data = self.dataList[indexPath.row]; // self.isNeedHold = YES; // if ([self.delegate respondsToSelector:@selector(searchGoToContent:)]) { // [self.delegate searchGoToContent:data]; // } //} @end