AllCoachPlanVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // AllCoachPlanVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 16/8/18.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "AllCoachPlanVC.h"
  9. #import "CoachPlanVC.h"
  10. #import "HolderView.h"
  11. #import "AllCoachCell.h"
  12. @interface AllCoachPlanVC ()<UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate,UISearchResultsUpdating,UISearchControllerDelegate>
  13. {
  14. HolderView *holderV;
  15. UITableView *mainTableView;
  16. UITextField *textField;
  17. UISearchBar *_searchBar;
  18. UISearchController *_searchController;
  19. BOOL isResult;
  20. NSArray *dataArray;
  21. NSMutableArray *resultArray;
  22. }
  23. @end
  24. @implementation AllCoachPlanVC
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. [self myInit];
  28. }
  29. -(void)viewWillDisappear:(BOOL)animated
  30. {
  31. [super viewWillDisappear:animated];
  32. [self.view endEditing:YES];
  33. [MBProgressHUD hideHUDForView:self.view];
  34. //这个一定要写 要不然会在退出去下一个页面时候 搜索栏有一定时间的存留
  35. if (_searchController.active) {
  36. _searchController.active = NO;
  37. if (_searchController.searchBar && _searchController.searchBar.superview) {
  38. [_searchController.searchBar removeFromSuperview];
  39. }
  40. }
  41. }
  42. -(void)myInit{
  43. [self setTitle:@"教练列表"];
  44. [self.view setBackgroundColor:KBackGroundColor];
  45. [self goBackByNavigation];
  46. isResult = NO;
  47. dataArray = [NSArray array];
  48. resultArray = [NSMutableArray array];
  49. mainTableView = [[UITableView alloc] initWithFrame:kFrame style:UITableViewStylePlain];
  50. mainTableView.height = kSize.height-kNavOffSet;
  51. mainTableView.delegate = self;
  52. mainTableView.dataSource = self;
  53. mainTableView.rowHeight = 110;
  54. mainTableView.tableFooterView = [UIView new];
  55. [self.view addSubview:mainTableView];
  56. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
  57. {
  58. _searchController =[[UISearchController alloc]initWithSearchResultsController:nil];
  59. _searchController.delegate = self;
  60. [_searchController.searchBar sizeToFit];
  61. _searchController.searchResultsUpdater = self;
  62. //这个是在搜索时候将之前的隐藏 包括表 无法点击 适合弹出新的tableviewcontroller 默认yes
  63. _searchController.dimsBackgroundDuringPresentation = NO;
  64. //这个是搜索时候隐藏导航栏
  65. _searchController.hidesNavigationBarDuringPresentation = NO;
  66. _searchController.searchBar.placeholder=@"请输入教练姓名或者电话";
  67. // _contactTable.sectionIndexColor = [UIColor blueColor];
  68. mainTableView.tableHeaderView=_searchController.searchBar;
  69. //[self searchBarTextDidBeginEditing:_searchController.searchBar];
  70. //不用弹出 因为没有单独的tableviewcontroller
  71. //[self presentViewController:_searchController animated:YES completion:nil];
  72. }else{
  73. //实例化一个搜索框
  74. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 40)];
  75. //设置背景色
  76. [_searchBar setBarTintColor:[UIColor grayColor]];
  77. //设置搜索框风格
  78. [_searchBar setSearchBarStyle:UISearchBarStyleProminent];
  79. [_searchBar setPlaceholder:@"请输入教练姓名或者电话"];
  80. //设置显示取消按钮
  81. [_searchBar setShowsCancelButton:YES animated:YES];
  82. //设置类型的标题
  83. //[_searchBar setScopeButtonTitles:@[@"按姓名查找",@"按电话查找"]];
  84. //改变索引的颜色
  85. //_contactTable.sectionIndexColor = [UIColor blueColor];
  86. //改变索引选中的背景颜色
  87. //设置表的表头
  88. [mainTableView setTableHeaderView:_searchBar];
  89. //搜索显示器 Display显示
  90. UISearchDisplayController * displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  91. [displayController setDelegate:self];
  92. [displayController setSearchResultsDataSource:self];
  93. [displayController setSearchResultsDelegate:self];
  94. }
  95. holderV = [[HolderView alloc] initWithFrame:mainTableView.frame];
  96. [holderV freshBlock:^{
  97. [self getCoachInfos];
  98. }];
  99. [self.view addSubview:holderV];
  100. [self getCoachInfos];
  101. }
  102. -(void)btnClick:(UIButton *)sender
  103. {
  104. [self.view endEditing:YES];
  105. [resultArray removeAllObjects];
  106. if (textField.text.length < 1) {
  107. isResult = NO;
  108. }else{
  109. isResult = YES;
  110. for (NSDictionary *dic in dataArray) {
  111. if ([dic[@"NAME"] containsString:textField.text] || [dic[@"CARTYPE"] containsString:textField.text] || [dic[@"SEX"] containsString:textField.text]) {
  112. [resultArray addObject:dic];
  113. }
  114. }
  115. }
  116. [mainTableView reloadData];
  117. }
  118. #pragma mark maintableview delegate
  119. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  120. {
  121. if (isResult) {
  122. return resultArray.count;
  123. }
  124. return dataArray.count;
  125. }
  126. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. AllCoachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allCoach"];
  129. if (cell == nil)
  130. {
  131. cell = [[AllCoachCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"allCoach"];
  132. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  133. }
  134. NSDictionary *dic;
  135. if (isResult) {
  136. dic = resultArray[indexPath.row];
  137. }else{
  138. dic = dataArray[indexPath.row];
  139. }
  140. cell.dataDic = dic;
  141. cell.superVC = self;
  142. return cell;
  143. }
  144. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. CoachPlanVC *vc = [[CoachPlanVC alloc] init];
  147. if (isResult) {
  148. vc.coachDic = resultArray[indexPath.row];
  149. }else{
  150. vc.coachDic = dataArray[indexPath.row];
  151. }
  152. [self navPushHideTabbarToVC:vc];
  153. }
  154. -(void)scrollViewDidScroll:(UIScrollView *)scrollView
  155. {
  156. //[self.view endEditing:YES];
  157. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
  158. {
  159. if (_searchController.searchBar) {
  160. [_searchController.searchBar endEditing:YES];
  161. }
  162. }else{
  163. if (_searchBar) {
  164. [_searchBar endEditing:YES];
  165. }
  166. }
  167. }
  168. #pragma mark SearchController Delegate
  169. -(void)willPresentSearchController:(UISearchController *)searchController{
  170. _searchController.searchBar.showsCancelButton = YES;
  171. UIButton *canceLBtn = [_searchController.searchBar valueForKey:@"cancelButton"];
  172. [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
  173. [canceLBtn setTitleColor:defGreen forState:UIControlStateNormal];
  174. }
  175. #pragma mark UISearchDisplayController 代理方法
  176. //iOS7搜索的时候 会调用这个方法
  177. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  178. {
  179. //NSLog(@"searchString====%@",searchString);
  180. [resultArray removeAllObjects];
  181. if (searchString.length < 1) {
  182. isResult = NO;
  183. }else{
  184. isResult = YES;
  185. for (NSDictionary *dic in dataArray) {
  186. NSString *numString = @"1234567890";
  187. if (searchString.length == 1 && [numString containsString:searchString]) {
  188. if ([dic[@"STAR"] integerValue] > [numString integerValue]) {
  189. [resultArray addObject:dic];
  190. }
  191. continue;
  192. }
  193. if ([dic[@"NAME"] containsString:searchString] || [dic[@"CARTYPE"] containsString:searchString] || [dic[@"SEX"] containsString:searchString] || [dic[@"MOBILE"] containsString:searchString]) {
  194. [resultArray addObject:dic];
  195. }
  196. }
  197. }
  198. [mainTableView reloadData];
  199. return YES;
  200. }
  201. //点击不同的选择范围的时候调用
  202. -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
  203. {
  204. [self searchDisplayController:controller shouldReloadTableForSearchString:_searchBar.text];
  205. return YES;
  206. }
  207. //iOS8搜索调用的方法
  208. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  209. {
  210. [resultArray removeAllObjects];
  211. NSString *searchString = _searchController.searchBar.text;
  212. if (searchString.length < 1) {
  213. isResult = NO;
  214. }else{
  215. isResult = YES;
  216. for (NSDictionary *dic in dataArray) {
  217. NSString *numString = @"1234567890";
  218. if (searchString.length == 1 && [numString containsString:searchString]) {
  219. if ([dic[@"STAR"] intValue] >= [searchString intValue]) {
  220. [resultArray addObject:dic];
  221. }
  222. continue;
  223. }
  224. if ([dic[@"NAME"] containsString:searchString] || [dic[@"CARTYPE"] containsString:searchString] || [dic[@"SEX"] containsString:searchString] || [dic[@"MOBILE"] containsString:searchString]) {
  225. [resultArray addObject:dic];
  226. }
  227. }
  228. }
  229. [mainTableView reloadData];
  230. }
  231. #pragma mark 异步获取教练信息
  232. -(void)getCoachInfos{
  233. if (![NetManager connectedToNetWork]) {
  234. showMsgUnconnect();
  235. return;
  236. }
  237. NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
  238. NSString *method = @"getSchPlaceCoachs";
  239. if (_fenceSiteID.length > 0) {
  240. [dic setValue:_fenceSiteID forKey:@"placeId"];
  241. }else {
  242. [dic setValue:defUser.userDict[@"school"] forKey:@"schid"];
  243. method = @"getCoachInfos";
  244. }
  245. [MBProgressHUD showLoadToView:self.view];
  246. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root){
  247. [MBProgressHUD hideHUDForView:self.view];
  248. [holderV setHidden:NO];
  249. if (!root) {
  250. ShowMsg(@"获取教练列表失败!");
  251. return;
  252. }
  253. if ([root[@"code"] isEqualToString:@"1"]) {
  254. ShowMsg(root[@"msg"]);
  255. return;
  256. }
  257. if ([root[@"body"] count] < 1) {
  258. return;
  259. }
  260. dataArray = root[@"body"];
  261. [holderV setHidden:YES];
  262. [mainTableView reloadData];
  263. }];
  264. }
  265. - (void)didReceiveMemoryWarning {
  266. [super didReceiveMemoryWarning];
  267. }
  268. @end