AllCoachPlanVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. RemoveHUD();
  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 __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
  57. //实例化一个搜索框
  58. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 40)];
  59. //设置背景色
  60. [_searchBar setBarTintColor:[UIColor grayColor]];
  61. //设置搜索框风格
  62. [_searchBar setSearchBarStyle:UISearchBarStyleProminent];
  63. [_searchBar setPlaceholder:@"请输入教练姓名或者电话"];
  64. //设置显示取消按钮
  65. [_searchBar setShowsCancelButton:YES animated:YES];
  66. //设置类型的标题
  67. //[_searchBar setScopeButtonTitles:@[@"按姓名查找",@"按电话查找"]];
  68. //改变索引的颜色
  69. //_contactTable.sectionIndexColor = [UIColor blueColor];
  70. //改变索引选中的背景颜色
  71. //设置表的表头
  72. [mainTableView setTableHeaderView:_searchBar];
  73. //搜索显示器 Display显示
  74. UISearchDisplayController * displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  75. [displayController setDelegate:self];
  76. [displayController setSearchResultsDataSource:self];
  77. [displayController setSearchResultsDelegate:self];
  78. #else
  79. _searchController =[[UISearchController alloc]initWithSearchResultsController:nil];
  80. _searchController.delegate = self;
  81. [_searchController.searchBar sizeToFit];
  82. _searchController.searchResultsUpdater = self;
  83. //这个是在搜索时候将之前的隐藏 包括表 无法点击 适合弹出新的tableviewcontroller 默认yes
  84. _searchController.dimsBackgroundDuringPresentation = NO;
  85. //这个是搜索时候隐藏导航栏
  86. _searchController.hidesNavigationBarDuringPresentation = NO;
  87. _searchController.searchBar.placeholder=@"请输入教练姓名或者电话";
  88. // _contactTable.sectionIndexColor = [UIColor blueColor];
  89. mainTableView.tableHeaderView=_searchController.searchBar;
  90. //[self searchBarTextDidBeginEditing:_searchController.searchBar];
  91. //不用弹出 因为没有单独的tableviewcontroller
  92. //[self presentViewController:_searchController animated:YES completion:nil];
  93. #endif
  94. holderV = [[HolderView alloc] initWithFrame:mainTableView.frame];
  95. [holderV freshBlock:^{
  96. [self getCoachInfos];
  97. }];
  98. [self.view addSubview:holderV];
  99. [self getCoachInfos];
  100. }
  101. -(void)btnClick:(UIButton *)sender
  102. {
  103. [self.view endEditing:YES];
  104. [resultArray removeAllObjects];
  105. if (textField.text.length < 1) {
  106. isResult = NO;
  107. }else{
  108. isResult = YES;
  109. for (NSDictionary *dic in dataArray) {
  110. if ([dic[@"NAME"] containsString:textField.text] || [dic[@"CARTYPE"] containsString:textField.text] || [dic[@"SEX"] containsString:textField.text]) {
  111. [resultArray addObject:dic];
  112. }
  113. }
  114. }
  115. [mainTableView reloadData];
  116. }
  117. #pragma mark maintableview delegate
  118. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  119. {
  120. if (isResult) {
  121. return resultArray.count;
  122. }
  123. return dataArray.count;
  124. }
  125. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. AllCoachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allCoach"];
  128. if (cell == nil)
  129. {
  130. cell = [[AllCoachCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"allCoach"];
  131. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  132. }
  133. NSDictionary *dic;
  134. if (isResult) {
  135. dic = resultArray[indexPath.row];
  136. }else{
  137. dic = dataArray[indexPath.row];
  138. }
  139. cell.dataDic = dic;
  140. cell.superVC = self;
  141. return cell;
  142. }
  143. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. CoachPlanVC *vc = [[CoachPlanVC alloc] init];
  146. if (isResult) {
  147. vc.coachDic = resultArray[indexPath.row];
  148. }else{
  149. vc.coachDic = dataArray[indexPath.row];
  150. }
  151. [self navPushHideTabbarToVC:vc];
  152. }
  153. -(void)scrollViewDidScroll:(UIScrollView *)scrollView
  154. {
  155. //[self.view endEditing:YES];
  156. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
  157. {
  158. if (_searchController.searchBar) {
  159. [_searchController.searchBar endEditing:YES];
  160. }
  161. }else{
  162. if (_searchBar) {
  163. [_searchBar endEditing:YES];
  164. }
  165. }
  166. }
  167. #pragma mark SearchController Delegate
  168. -(void)willPresentSearchController:(UISearchController *)searchController{
  169. _searchController.searchBar.showsCancelButton = YES;
  170. UIButton *canceLBtn = [_searchController.searchBar valueForKey:@"cancelButton"];
  171. [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
  172. [canceLBtn setTitleColor:RQMianColor forState:UIControlStateNormal];
  173. }
  174. #pragma mark UISearchDisplayController 代理方法
  175. //iOS7搜索的时候 会调用这个方法
  176. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
  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. #endif
  208. //iOS8搜索调用的方法
  209. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  210. {
  211. [resultArray removeAllObjects];
  212. NSString *searchString = _searchController.searchBar.text;
  213. if (searchString.length < 1) {
  214. isResult = NO;
  215. }else{
  216. isResult = YES;
  217. for (NSDictionary *dic in dataArray) {
  218. NSString *numString = @"1234567890";
  219. if (searchString.length == 1 && [numString containsString:searchString]) {
  220. if ([dic[@"STAR"] intValue] >= [searchString intValue]) {
  221. [resultArray addObject:dic];
  222. }
  223. continue;
  224. }
  225. if ([dic[@"NAME"] containsString:searchString] || [dic[@"CARTYPE"] containsString:searchString] || [dic[@"SEX"] containsString:searchString] || [dic[@"MOBILE"] containsString:searchString]) {
  226. [resultArray addObject:dic];
  227. }
  228. }
  229. }
  230. [mainTableView reloadData];
  231. }
  232. #pragma mark 异步获取教练信息
  233. -(void)getCoachInfos{
  234. if (![NetManager connectedToNetWork]) {
  235. showMsgUnconnect();
  236. return;
  237. }
  238. NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
  239. NSString *method = @"getSchPlaceCoachs";
  240. if (_fenceSiteID.length > 0) {
  241. [dic setValue:_fenceSiteID forKey:@"placeId"];
  242. }else {
  243. [dic setValue:defUser.userDict[@"school"] forKey:@"schid"];
  244. method = @"getCoachInfos";
  245. }
  246. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root){
  247. [holderV setHidden:NO];
  248. if (!root) {
  249. ShowMsg(@"获取教练列表失败!");
  250. return;
  251. }
  252. if ([root[@"code"] isEqualToString:@"1"]) {
  253. ShowMsg(root[@"msg"]);
  254. return;
  255. }
  256. if ([root[@"body"] count] < 1) {
  257. return;
  258. }
  259. dataArray = root[@"body"];
  260. [holderV setHidden:YES];
  261. [mainTableView reloadData];
  262. }];
  263. }
  264. - (void)didReceiveMemoryWarning {
  265. [super didReceiveMemoryWarning];
  266. }
  267. @end