BaseIDAndNameViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // IDAndNameViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by apple on 2017/5/31.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "BaseIDAndNameViewController.h"
  9. #import "BaseIDAndNameModel.h"
  10. @interface BaseIDAndNameViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
  11. @property(nonatomic,strong) UISearchBar *searchBar;
  12. @property(nonatomic,strong) UITableView *tableView;
  13. @end
  14. @implementation BaseIDAndNameViewController
  15. //zhushi
  16. #pragma mark - 公共函数
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.navigationItem.title=@"品牌列表";
  20. //是否是dismissViewController
  21. if (_isPresentViewFlg) {
  22. UIView *vTitle = [[UIView alloc]init];
  23. vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight);
  24. vTitle.backgroundColor = NavigationBarTintColor;
  25. [self.view addSubview:vTitle];
  26. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  27. [button setImage:[UIImage imageNamed:@"icon_back"]
  28. forState:UIControlStateNormal];
  29. [button addTarget:self action:@selector(goBack)
  30. forControlEvents:UIControlEventTouchUpInside];
  31. button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15);
  32. [vTitle addSubview:button];
  33. UILabel *lblTitle = [[UILabel alloc]init];
  34. lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15);
  35. lblTitle.textColor = [UIColor whiteColor];
  36. lblTitle.text = @"请选择";
  37. lblTitle.textAlignment = NSTextAlignmentCenter;
  38. [vTitle addSubview:lblTitle];
  39. }else{
  40. self.navigationItem.title=@"请选择";
  41. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  42. [button setImage:[UIImage imageNamed:@"icon_back"]
  43. forState:UIControlStateNormal];
  44. [button addTarget:self action:@selector(goBack)
  45. forControlEvents:UIControlEventTouchUpInside];
  46. button.frame = CGRectMake(0, 0, 15, 18);
  47. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  48. self.navigationItem.leftBarButtonItem = menuButton;
  49. [self.navigationController setNavigationBarHidden:NO];
  50. }
  51. _tableView=[UITableView new];
  52. _tableView.delegate=self;
  53. _tableView.dataSource=self;
  54. //是否是dismissViewController
  55. if (_isPresentViewFlg) {
  56. self.tableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ;
  57. }
  58. else{
  59. self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ;
  60. }
  61. [self.view addSubview:_tableView];
  62. //搜索框
  63. _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
  64. _searchBar.delegate = self;
  65. self.tableView.tableHeaderView=_searchBar;
  66. }
  67. /**
  68. didReceiveMemoryWarning
  69. */
  70. - (void)didReceiveMemoryWarning {
  71. [super didReceiveMemoryWarning];
  72. }
  73. /**
  74. viewWillAppear
  75. @param animated <#animated description#>
  76. */
  77. - (void)viewWillAppear:(BOOL)animated
  78. {
  79. [self.tableView reloadData];
  80. }
  81. /**
  82. dealloc
  83. */
  84. - (void)dealloc
  85. {
  86. [self cancel];
  87. }
  88. #pragma mark - 委托回调函数
  89. #pragma mark - tableView回调
  90. /**
  91. Sections
  92. @param tableView <#tableView description#>
  93. @return <#return value description#>
  94. */
  95. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  96. {
  97. return 1;
  98. }
  99. /**
  100. Sections的行数
  101. @param tableView <#tableView description#>
  102. @param section <#section description#>
  103. @return <#return value description#>
  104. */
  105. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  106. {
  107. return _filterArr.count;
  108. }
  109. /**
  110. 单元格cell
  111. @param tableView <#tableView description#>
  112. @param indexPath <#indexPath description#>
  113. @return <#return value description#>
  114. */
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. static NSString *CellIdentifier = @"Cell";
  118. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
  119. if(cell==nil)
  120. {
  121. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  122. }
  123. BaseIDAndNameModel *model = _filterArr[indexPath.row];
  124. // cell.tag=[[dic objectForKey:@"BrandID"] intValue];
  125. cell.textLabel.text = model.name;
  126. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  127. cell.textLabel.font = [UIFont systemFontOfSize:SubControllerListFontOfSize];
  128. return cell;
  129. }
  130. /**
  131. 单元格点击事件
  132. @param tableView <#tableView description#>
  133. @param indexPath <#indexPath description#>
  134. */
  135. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  136. BaseIDAndNameModel *model = _filterArr[indexPath.row];
  137. if ([self.bDelegate respondsToSelector:@selector(baseIDAndNameDoneDatas : BaseIDAndName:)]){
  138. [self.bDelegate baseIDAndNameDoneDatas:model BaseIDAndName:self.showDialogViewTag];
  139. }
  140. //是否是dismissViewController
  141. if (_isPresentViewFlg) {
  142. [self dismissViewControllerAnimated:YES completion:nil];
  143. }else{
  144. [self.navigationController popViewControllerAnimated:YES];
  145. }
  146. }
  147. #pragma mark searchbar回调
  148. /**
  149. 查询取消按钮添加
  150. @param searchBar <#searchBar description#>
  151. @return <#return value description#>
  152. */
  153. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  154. {
  155. [self addCancelButton];
  156. return YES;
  157. }
  158. /**
  159. shouldChangeTextInRange
  160. @param searchBar <#searchBar description#>
  161. @param range <#range description#>
  162. @param text <#text description#>
  163. @return <#return value description#>
  164. */
  165. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  166. {
  167. return YES;
  168. }
  169. /**
  170. 查询文本变化
  171. @param searchBar <#searchBar description#>
  172. @param searchText <#searchText description#>
  173. */
  174. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  175. {
  176. if ([searchText isEqualToString:@""]) {
  177. self.filterArr = _searchArr;
  178. [_tableView reloadData];
  179. return;
  180. }
  181. NSString *keyName = @"name";
  182. //< 模糊查找
  183. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", keyName, searchText];
  184. //< 精确查找
  185. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  186. NSLog(@"predicate %@",predicateString);
  187. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  188. self.filterArr = filteredArray;
  189. [_tableView reloadData];
  190. }
  191. /**
  192. 点击事件
  193. @param searchBar <#searchBar description#>
  194. */
  195. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  196. {
  197. [searchBar resignFirstResponder];
  198. [self cancelSearch];
  199. }
  200. /**
  201. scrollViewWillBeginDragging
  202. @param scrollView <#scrollView description#>
  203. */
  204. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  205. {
  206. [_searchBar resignFirstResponder];
  207. [self cancelSearch];
  208. }
  209. #pragma mark - 私有函数
  210. /**
  211. 进度条隐藏
  212. */
  213. - (void)cancel {
  214. [self stopLoading];
  215. }
  216. /**
  217. 添加取消按钮
  218. */
  219. - (void)addCancelButton
  220. {
  221. //添加取消按钮
  222. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  223. [button addTarget:self action:@selector(cancelSearch)
  224. forControlEvents:UIControlEventTouchUpInside];
  225. button.frame = CGRectMake(0, 0, 40, 24);
  226. [button setTitle:@"取消" forState:UIControlStateNormal];
  227. [button setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  228. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  229. self.navigationItem.rightBarButtonItem = menuButton;
  230. }
  231. /**
  232. 取消查询
  233. */
  234. - (void)cancelSearch
  235. {
  236. [_searchBar resignFirstResponder];
  237. [self removeCancel];
  238. }
  239. /**
  240. 取消按钮隐藏
  241. */
  242. - (void) removeCancel
  243. {
  244. self.navigationItem.rightBarButtonItem = nil;
  245. }
  246. /**
  247. 返回父界面
  248. */
  249. - (void)goBack
  250. {
  251. //是否是dismissViewController
  252. if (_isPresentViewFlg) {
  253. [self dismissViewControllerAnimated:YES completion:nil];
  254. }else{
  255. [self.navigationController popViewControllerAnimated:YES];
  256. }
  257. }
  258. @end