ChannelSearchVC.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // QuDaoSearchTC.m
  3. // IBOSSIPAD
  4. //
  5. // Created by iHope on 14-8-14.
  6. // Copyright (c) 2014年 elongtian. All rights reserved.
  7. //
  8. #import "ChannelSearchVC.h"
  9. #define textFont [UIFont systemFontOfSize:14]
  10. @interface ChannelSearchVC()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
  11. @property(nonatomic,strong) ASIDownManager *mDownManager;
  12. @property(strong) IBOutlet UISearchBar *searchBar;
  13. @property(strong) UITableView *tableView;
  14. @end
  15. @implementation ChannelSearchVC
  16. @synthesize subview;
  17. #pragma mark - 公共函数
  18. /**
  19. viewWillAppear函数
  20. */
  21. -(void)viewWillAppear:(BOOL)animated
  22. {
  23. _searchArr=[NSMutableArray new];
  24. _filterArr=[NSMutableArray new];
  25. self.mDownManager = [[ASIDownManager alloc] init];
  26. self.mDownManager = [[ASIDownManager alloc] init];
  27. _mDownManager.delegate = self;
  28. _mDownManager.OnImageDown = @selector(onLoadFinish:);
  29. _mDownManager.OnImageFail = @selector(onLoadFail:);
  30. [self reloadData];
  31. }
  32. /**
  33. viewDidLoad函数
  34. */
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. self.navigationItem.title=@"渠道";
  39. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  40. [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"]
  41. forState:UIControlStateNormal];
  42. [button addTarget:self action:@selector(goBack)
  43. forControlEvents:UIControlEventTouchUpInside];
  44. button.frame = CGRectMake(0, 0, 15, 18);
  45. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  46. self.navigationItem.leftBarButtonItem = menuButton;
  47. self.navigationController.navigationBar.barTintColor = NavigationBarTintColor;
  48. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]};
  49. _tableView=[UITableView new];
  50. _tableView.delegate=self;
  51. _tableView.dataSource=self;
  52. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  53. _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ;
  54. [self.view addSubview:_tableView];
  55. //搜索框
  56. _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
  57. _searchBar.delegate=self;
  58. self.tableView.tableHeaderView=_searchBar;
  59. }
  60. /**
  61. dealloc函数
  62. */
  63. -(void)dealloc
  64. {
  65. [self cancel];
  66. }
  67. #pragma mark - 委托函数
  68. //数据加载完成函数
  69. - (void)onLoadFinish:(ASIDownManager *)sender {
  70. NSDictionary *dic = [sender.mWebStr JSONValue];
  71. [self cancel];
  72. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  73. int iStatus = [[dic objectForKey:@"Status"] intValue];
  74. NSString *message=[dic objectForKey:@"Message"];
  75. if (iStatus == 0) {
  76. NSArray * approvArr=[dic objectForKey:@"Result"];
  77. if(approvArr!=nil)
  78. {
  79. _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
  80. _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
  81. if(_searchArr.count==0)
  82. {
  83. [self showAlertViewText:@"未找到匹配结果"];
  84. }
  85. [self.tableView reloadData];
  86. }
  87. }
  88. else if(iStatus==ActionResultStatusAuthError
  89. ||iStatus==ActionResultStatusNoLogin
  90. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  91. [self showReLoginDialog:message];
  92. return;
  93. }
  94. else {
  95. [self.tableView reloadData];
  96. [self showAlertViewText:message];
  97. }
  98. }
  99. }
  100. //数据加载失败函数
  101. - (void)onLoadFail:(ASIDownManager *)sender {
  102. [self cancel];
  103. [self.tableView reloadData];
  104. [self showAlertViewText:@"加载失败"];
  105. }
  106. //tableview的分区数
  107. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  108. {
  109. return 1;
  110. }
  111. //tableview的行数
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  113. {
  114. return _filterArr.count;
  115. }
  116. //获取tableviewcell
  117. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. static NSString *CellIdentifier = @"Cell";
  120. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
  121. if(cell==nil)
  122. {
  123. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  124. }
  125. NSDictionary *dic=_filterArr[indexPath.row];
  126. NSString *name=[dic objectForKey:@"ChannelName"];
  127. cell.textLabel.text=name;
  128. cell.textLabel.font=textFont;
  129. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  130. return cell;
  131. }
  132. //选中tableviewcell
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  134. if(!subview){
  135. [self dismissViewControllerAnimated:YES completion:^(void){
  136. NSDictionary *dic=_filterArr[indexPath.row];
  137. NSString * channelName=[dic objectForKey:@"ChannelName"];
  138. NSString *channelId=[dic objectForKey:@"ChannelID"];
  139. if([self.channelDelegate respondsToSelector:@selector(channel:channelName:)]){
  140. [self.channelDelegate channel:channelId channelName:channelName];
  141. }
  142. }];
  143. }
  144. else {
  145. NSDictionary *dic=_filterArr[indexPath.row];
  146. NSString * name=[dic objectForKey:@"ChannelName"];
  147. NSString *channelID=[dic objectForKey:@"ChannelID"];
  148. if([self.channelDelegate respondsToSelector:@selector(channel:channelName:)]){
  149. [self.channelDelegate channel:channelID channelName:name];
  150. }
  151. [self.navigationController popViewControllerAnimated:YES];
  152. }
  153. }
  154. /**
  155. 搜索栏开始编辑函数
  156. @param searchBar <#searchBar description#>
  157. @return <#return value description#>
  158. */
  159. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  160. {
  161. [self addCancelButton];
  162. return YES;
  163. }
  164. /**
  165. 是否允许替换文本
  166. @param searchBar <#searchBar description#>
  167. @param range <#range description#>
  168. @param text <#text description#>
  169. @return <#return value description#>
  170. */
  171. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  172. {
  173. return YES;
  174. }
  175. /**
  176. 搜索栏文本输入结束回调函数
  177. @param searchBar <#searchBar description#>
  178. @param searchText <#searchText description#>
  179. */
  180. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  181. {
  182. if ([searchText isEqualToString:@""]) {
  183. self.filterArr = _searchArr;
  184. [_tableView reloadData];
  185. return;
  186. }
  187. NSString *keyName = @"";
  188. // if (_searchState == DataSearchStateBank) {
  189. keyName = @"ChannelName";
  190. //}
  191. //< 模糊查找
  192. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"OrganizationCode",searchText];
  193. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"ChannelName",searchText];
  194. //< 精确查找
  195. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  196. NSLog(@"predicate %@",predicateString);
  197. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  198. self.filterArr = filteredArray;
  199. [_tableView reloadData];
  200. }
  201. /**
  202. 取消搜索函数
  203. @param searchBar <#searchBar description#>
  204. */
  205. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  206. {
  207. [searchBar resignFirstResponder];
  208. [self cancelSearch];
  209. }
  210. /**
  211. scrollView回调函数
  212. @param scrollView <#scrollView description#>
  213. */
  214. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  215. {
  216. [_searchBar resignFirstResponder];
  217. [self cancelSearch];
  218. }
  219. #pragma mark - 私有函数
  220. /**
  221. 取消进度条函数
  222. */
  223. - (void)cancel {
  224. [self stopLoading];
  225. }
  226. /**
  227. 加载数据函数
  228. */
  229. -(void)reloadData
  230. {
  231. [self startLoading];
  232. NSString *urlStr = ServerURL;
  233. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  234. [dict setObject:@"GetDataSource" forKey:@"Action"];
  235. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  236. [dict setObject:kkUserCode forKey:@"UserCode"];
  237. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  238. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  239. [dict setObject:@"T_MST_Channel" forKey:@"DataSourceCode"];
  240. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];}
  241. /**
  242. 返回函数
  243. */
  244. -(void)goBack
  245. {
  246. if(!subview){
  247. [self dismissViewControllerAnimated:YES completion:^(void){
  248. }];}
  249. else{
  250. [self.navigationController popViewControllerAnimated:YES];
  251. }
  252. }
  253. /**
  254. 添加取消按钮
  255. */
  256. -(void)addCancelButton
  257. {
  258. UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
  259. [button2 addTarget:self action:@selector(cancelSearch)
  260. forControlEvents:UIControlEventTouchUpInside];
  261. button2.frame = CGRectMake(0, 0, 40, 24);
  262. [button2 setTitle:@"取消" forState:UIControlStateNormal];
  263. [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  264. UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
  265. self.navigationItem.rightBarButtonItem = menuButton2;
  266. }
  267. /**
  268. 取消搜索
  269. */
  270. -(void)cancelSearch
  271. {
  272. [_searchBar resignFirstResponder];
  273. [self removeCancel];
  274. }
  275. /**
  276. 移除取消按钮
  277. */
  278. -(void) removeCancel
  279. {
  280. self.navigationItem.rightBarButtonItem = nil;
  281. }
  282. @end