SYBase_tbVC.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // SYBase_tbVC.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/7.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "SYBase_tbVC.h"
  9. @interface SYBase_tbVC ()
  10. @end
  11. @implementation SYBase_tbVC
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. self.view.backgroundColor = KBackGroundColor;
  15. _dataSource = [[NSMutableArray alloc]init];
  16. _sectionTitles = [[NSMutableArray alloc]init];
  17. _requsetDic = [[NSMutableDictionary alloc]init];
  18. _resultArray = [[NSMutableArray alloc] init];
  19. _hasSearch = NO;
  20. [self myInit];
  21. }
  22. -(void)viewWillDisappear:(BOOL)animated
  23. {
  24. [super viewWillDisappear:animated];
  25. if (_searchController.active) {
  26. _searchController.active = NO;
  27. if (_searchController.searchBar && _searchController.searchBar.superview) {
  28. [_searchController.searchBar removeFromSuperview];
  29. }
  30. }
  31. }
  32. -(void)myInit{
  33. _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
  34. _tableView.dataSource = self;
  35. _tableView.delegate = self;
  36. [self.view addSubview:_tableView];
  37. self.tableView.tableFooterView = [UIView new];
  38. [self.tableView registerClass:[SelectCell class] forCellReuseIdentifier:@"cellId"];
  39. //设置索引条颜色
  40. // self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
  41. self.tableView.sectionIndexColor = COLOR_THEME;
  42. //
  43. _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
  44. _searchController.searchResultsUpdater = self;
  45. _searchController.searchBar.placeholder = @"请输入姓名/证件号/电话";
  46. //设置UISearchController的显示属性,以下3个属性默认为YES
  47. //搜索时,背景变暗色
  48. _searchController.dimsBackgroundDuringPresentation = NO;
  49. //搜索时,背景变模糊
  50. // _searchController.obscuresBackgroundDuringPresentation = NO;
  51. //隐藏导航栏
  52. _searchController.hidesNavigationBarDuringPresentation = NO;
  53. [_searchController.searchBar sizeToFit];
  54. _searchController.searchBar.frame = CGRectMake(_searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
  55. self.tableView.tableHeaderView = _searchController.searchBar;
  56. self.holderV = [[HolderView alloc] initWithFrame:_tableView.frame];
  57. [self.holderV freshBlock:^{
  58. if (_block) {
  59. _block();
  60. }
  61. }];
  62. [self.view addSubview:self.holderV];
  63. }
  64. #pragma mark 获取数据
  65. -(void)getDataWithDic:(NSMutableDictionary *)mdic method:(NSString *)method block:(Block)block{
  66. //判断网络是否连接
  67. if (![NetworkManager connectedToNetWork]) {
  68. [self showMsgByAlertVCWithString:@"网络连接异常"];
  69. return;
  70. }
  71. [MBProgressHUD hideHUDForView:self.view animated:NO];
  72. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  73. [NetworkManager requestWithMethod:method parameters:mdic type:0 handler:^(NSDictionary *successDic, NSString *failureStr) {
  74. [MBProgressHUD hideHUDForView:self.view animated:YES];
  75. [self.holderV setHidden:NO];
  76. if (failureStr) {
  77. [self showMsgByAlertVCWithString:failureStr];
  78. return;
  79. }
  80. if ([successDic[@"code"] isEqualToString:@"1"]) {
  81. [self showMsgByAlertVCWithString:successDic[@"msg"]];
  82. return;
  83. }
  84. if (block) {
  85. block(successDic);
  86. }
  87. }];
  88. }
  89. #pragma mark 弹出框提示
  90. - (void)showMsgByAlertVCWithString:(NSString *)str{
  91. UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
  92. [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
  93. [self presentViewController:alertFind animated:true completion:nil];
  94. }
  95. - (void)showMsgByMBWithString:(NSString *)str{
  96. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  97. hud.mode = MBProgressHUDModeText;
  98. hud.label.text = str;
  99. hud.margin = HUD_MARGIN;
  100. hud.removeFromSuperViewOnHide = YES;
  101. [hud hideAnimated:YES afterDelay:2];
  102. }
  103. #pragma mark 代理方法
  104. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  105. if (_hasSearch) {
  106. return 1;
  107. }
  108. return _dataSource.count;
  109. }
  110. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  111. if (_hasSearch) {
  112. return _resultArray.count;
  113. }
  114. return [_dataSource[section] count];
  115. }
  116. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  117. return 44;
  118. }
  119. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"unuse"];
  122. if (!cell) {
  123. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"unuse"];
  124. }
  125. return cell;
  126. }
  127. // 每个分区的页眉
  128. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  129. {
  130. if (_hasSearch) {
  131. return nil;
  132. }
  133. return [_sectionTitles objectAtIndex:section];
  134. }
  135. // 索引目录
  136. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  137. {
  138. if (_hasSearch) {
  139. return nil;
  140. }
  141. return _sectionTitles;
  142. }
  143. // 点击目录(响应索引点击)
  144. -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  145. {
  146. // 获取所点目录对应的indexPath值
  147. NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
  148. // 让table滚动到对应的indexPath位置
  149. [tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  150. return index;
  151. }
  152. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
  153. //消除黄色警告
  154. }
  155. - (void)didReceiveMemoryWarning {
  156. [super didReceiveMemoryWarning];
  157. // Dispose of any resources that can be recreated.
  158. }
  159. @end