ResponsiblePersonViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // ResponsiblePersonViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by apple on 2017/5/27.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "ResponsiblePersonViewController.h"
  9. #import "ResponsiblePersonCell.h"
  10. @interface ResponsiblePersonViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
  11. @property(nonatomic,strong) UISearchBar *searchBar;
  12. @property(nonatomic,strong) UITableView *tableViewPerson;
  13. @end
  14. @implementation ResponsiblePersonViewController
  15. #pragma mark - 公共事件
  16. /**
  17. viewDidLoad事件
  18. */
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.navigationItem.title=@"请选择";
  22. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  23. [button setImage:[UIImage imageNamed:@"icon_back"]
  24. forState:UIControlStateNormal];
  25. [button addTarget:self action:@selector(sGotoBack)
  26. forControlEvents:UIControlEventTouchUpInside];
  27. button.frame = CGRectMake(0, 0, 15, 18);
  28. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  29. self.navigationItem.leftBarButtonItem = menuButton;
  30. [self.navigationController setNavigationBarHidden:NO];
  31. self.tableViewPerson=[UITableView new];
  32. self.tableViewPerson.dataSource=self;
  33. self.tableViewPerson.delegate=self;
  34. self.tableViewPerson.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ;
  35. [self.view addSubview:self.tableViewPerson];
  36. //搜索框
  37. self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
  38. self.searchBar.delegate=self;
  39. self.tableViewPerson.tableHeaderView = self.searchBar;
  40. }
  41. /**
  42. viewWillAppear事件
  43. @param animated <#animated description#>
  44. */
  45. -(void)viewWillAppear:(BOOL)animated{
  46. if(!_filterArr){
  47. _filterArr=[NSMutableArray new];
  48. }
  49. if(!_searchArr){
  50. _searchArr=[NSMutableArray new];
  51. }
  52. [self reloadData];
  53. }
  54. /**
  55. dealloc事件
  56. */
  57. -(void)dealloc{
  58. [self cancel];
  59. }
  60. #pragma mark - 回调函数
  61. #pragma mark - tableveiw回调函数
  62. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  63. UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  64. NSLog(@"cell height %f",cell.frame.size.height);
  65. return cell.frame.size.height;
  66. }
  67. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  68. static NSString *CellIdentifier = @"ResponsiblePersonCell";
  69. ResponsiblePersonCell *cell =(ResponsiblePersonCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  70. if(cell==nil){
  71. NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:CellIdentifier owner:nil options:nil];
  72. cell = [nibs lastObject];
  73. cell.backgroundColor = [UIColor clearColor];
  74. // cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y,self.view.bounds.size.width , cell.frame.size.height);
  75. }
  76. ResponsiblePersonModel *info=self.filterArr[indexPath.row];
  77. cell.lblCode.text = info.staffCode;
  78. cell.lblName.text = info.staffName;
  79. cell.staffid = info.staffID;
  80. return cell;
  81. }
  82. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  83. ResponsiblePersonModel *info=self.filterArr[indexPath.row];
  84. if ([self.respDelegate respondsToSelector:@selector(updateResponsiblePerson:)]) {
  85. [self.respDelegate updateResponsiblePerson:info];
  86. }
  87. [self.navigationController popViewControllerAnimated:YES];
  88. }
  89. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  90. return _filterArr.count;
  91. }
  92. #pragma mark - 数据回调函数
  93. #pragma mark searchbar回调
  94. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  95. {
  96. [self addCancelButton];
  97. return YES;
  98. }
  99. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  100. {
  101. return YES;
  102. }
  103. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  104. {
  105. if ([searchText isEqualToString:@""]) {
  106. self.filterArr = _searchArr;
  107. [self.tableViewPerson reloadData];
  108. return;
  109. }
  110. NSString *keyName = @"name";
  111. //< 模糊查找
  112. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K CONTAINS[cd] %@", keyName, searchText];
  113. //< 精确查找
  114. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  115. NSLog(@"predicate %@",predicateString);
  116. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  117. self.filterArr = filteredArray;
  118. [self changeArrToModel:_filterArr];
  119. [self.tableViewPerson reloadData];
  120. }
  121. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  122. {
  123. [searchBar resignFirstResponder];
  124. [self cancelSearch];
  125. }
  126. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  127. {
  128. [_searchBar resignFirstResponder];
  129. [self cancelSearch];
  130. }
  131. #pragma mark - 私有函数
  132. /**
  133. 进度条隐藏
  134. */
  135. - (void)cancel {
  136. [self stopLoading];
  137. }
  138. /**
  139. 返回事件
  140. */
  141. -(void)sGotoBack
  142. {
  143. [self dismissViewControllerAnimated:YES completion:nil];
  144. [self.navigationController popViewControllerAnimated:YES];
  145. }
  146. /**
  147. 数据加载
  148. */
  149. -(void)reloadData
  150. {
  151. [self.tableViewPerson reloadData ];
  152. }
  153. /**
  154. 取消按钮
  155. */
  156. -(void)addCancelButton
  157. {
  158. //添加取消按钮
  159. UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
  160. [button2 addTarget:self action:@selector(cancelSearch)
  161. forControlEvents:UIControlEventTouchUpInside];
  162. button2.frame = CGRectMake(0, 0, 40, 24);
  163. [button2 setTitle:@"取消" forState:UIControlStateNormal];
  164. [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  165. UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
  166. self.navigationItem.rightBarButtonItem = menuButton2;
  167. }
  168. /**
  169. 查询取消
  170. */
  171. -(void)cancelSearch
  172. {
  173. [_searchBar resignFirstResponder];
  174. [self removeCancel];
  175. }
  176. /**
  177. 取消隐藏
  178. */
  179. -(void) removeCancel
  180. {
  181. self.navigationItem.rightBarButtonItem = nil;
  182. }
  183. /**
  184. 数据model
  185. @param arr <#arr description#>
  186. */
  187. -(void)changeArrToModel:(NSArray* )arr{
  188. if(arr!=nil){
  189. }
  190. }
  191. @end