// // QuDaoSearchTC.m // IBOSSIPAD // // Created by iHope on 14-8-14. // Copyright (c) 2014年 elongtian. All rights reserved. // #import "ChannelSearchVC.h" #define textFont [UIFont systemFontOfSize:14] @interface ChannelSearchVC() @property(nonatomic,strong) ASIDownManager *mDownManager; @property(strong) IBOutlet UISearchBar *searchBar; @property(strong) UITableView *tableView; @end @implementation ChannelSearchVC @synthesize subview; #pragma mark - 公共函数 /** viewWillAppear函数 */ -(void)viewWillAppear:(BOOL)animated { _searchArr=[NSMutableArray new]; _filterArr=[NSMutableArray new]; self.mDownManager = [[ASIDownManager alloc] init]; self.mDownManager = [[ASIDownManager alloc] init]; _mDownManager.delegate = self; _mDownManager.OnImageDown = @selector(onLoadFinish:); _mDownManager.OnImageFail = @selector(onLoadFail:); [self reloadData]; } /** viewDidLoad函数 */ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"渠道"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; self.navigationController.navigationBar.barTintColor = NavigationBarTintColor; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]}; _tableView=[UITableView new]; _tableView.delegate=self; _tableView.dataSource=self; _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ; [self.view addSubview:_tableView]; //搜索框 _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; _searchBar.delegate=self; self.tableView.tableHeaderView=_searchBar; } /** dealloc函数 */ -(void)dealloc { [self cancel]; } #pragma mark - 委托函数 //数据加载完成函数 - (void)onLoadFinish:(ASIDownManager *)sender { NSDictionary *dic = [sender.mWebStr JSONValue]; [self cancel]; if (dic && [dic isKindOfClass:[NSDictionary class]]) { int iStatus = [[dic objectForKey:@"Status"] intValue]; NSString *message=[dic objectForKey:@"Message"]; if (iStatus == 0) { NSArray * approvArr=[dic objectForKey:@"Result"]; if(approvArr!=nil) { _searchArr=[[NSMutableArray alloc]initWithArray:approvArr]; _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr]; if(_searchArr.count==0) { [self showAlertViewText:@"未找到匹配结果"]; } [self.tableView reloadData]; } } else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; return; } else { [self.tableView reloadData]; [self showAlertViewText:message]; } } } //数据加载失败函数 - (void)onLoadFail:(ASIDownManager *)sender { [self cancel]; [self.tableView reloadData]; [self showAlertViewText:@"加载失败"]; } //tableview的分区数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //tableview的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _filterArr.count; } //获取tableviewcell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ]; if(cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } NSDictionary *dic=_filterArr[indexPath.row]; NSString *name=[dic objectForKey:@"ChannelName"]; cell.textLabel.text=name; cell.textLabel.font=textFont; cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; } //选中tableviewcell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(!subview){ [self dismissViewControllerAnimated:YES completion:^(void){ NSDictionary *dic=_filterArr[indexPath.row]; NSString * channelName=[dic objectForKey:@"ChannelName"]; NSString *channelId=[dic objectForKey:@"ChannelID"]; if([self.channelDelegate respondsToSelector:@selector(channel:channelName:)]){ [self.channelDelegate channel:channelId channelName:channelName]; } }]; } else { NSDictionary *dic=_filterArr[indexPath.row]; NSString * name=[dic objectForKey:@"ChannelName"]; NSString *channelID=[dic objectForKey:@"ChannelID"]; if([self.channelDelegate respondsToSelector:@selector(channel:channelName:)]){ [self.channelDelegate channel:channelID channelName:name]; } [self.navigationController popViewControllerAnimated:YES]; } } /** 搜索栏开始编辑函数 @param searchBar <#searchBar description#> @return <#return value description#> */ - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [self addCancelButton]; return YES; } /** 是否允许替换文本 @param searchBar <#searchBar description#> @param range <#range description#> @param text <#text description#> @return <#return value description#> */ - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; } /** 搜索栏文本输入结束回调函数 @param searchBar <#searchBar description#> @param searchText <#searchText description#> */ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if ([searchText isEqualToString:@""]) { self.filterArr = _searchArr; [_tableView reloadData]; return; } NSString *keyName = @""; // if (_searchState == DataSearchStateBank) { keyName = @"ChannelName"; //} //< 模糊查找 // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"OrganizationCode",searchText]; NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"ChannelName",searchText]; //< 精确查找 // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText]; NSLog(@"predicate %@",predicateString); NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]]; self.filterArr = filteredArray; [_tableView reloadData]; } /** 取消搜索函数 @param searchBar <#searchBar description#> */ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; [self cancelSearch]; } /** scrollView回调函数 @param scrollView <#scrollView description#> */ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [_searchBar resignFirstResponder]; [self cancelSearch]; } #pragma mark - 私有函数 /** 取消进度条函数 */ - (void)cancel { [self stopLoading]; } /** 加载数据函数 */ -(void)reloadData { [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"GetDataSource" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [dict setObject:@"T_MST_Channel" forKey:@"DataSourceCode"]; [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];} /** 返回函数 */ -(void)goBack { if(!subview){ [self dismissViewControllerAnimated:YES completion:^(void){ }];} else{ [self.navigationController popViewControllerAnimated:YES]; } } /** 添加取消按钮 */ -(void)addCancelButton { UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; [button2 addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside]; button2.frame = CGRectMake(0, 0, 40, 24); [button2 setTitle:@"取消" forState:UIControlStateNormal]; [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal]; UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2]; self.navigationItem.rightBarButtonItem = menuButton2; } /** 取消搜索 */ -(void)cancelSearch { [_searchBar resignFirstResponder]; [self removeCancel]; } /** 移除取消按钮 */ -(void) removeCancel { self.navigationItem.rightBarButtonItem = nil; } @end