EMSearchResultController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // EMSearchResultController.m
  3. // DXStudio
  4. //
  5. // Created by XieYajie on 22/09/2017.
  6. // Copyright © 2017 dxstudio. All rights reserved.
  7. //
  8. #import "EMSearchResultController.h"
  9. #import "MJRefresh.h"
  10. @interface EMSearchResultController ()<UITableViewDelegate, UITableViewDataSource>
  11. @end
  12. @implementation EMSearchResultController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. // Uncomment the following line to preserve selection between presentations.
  16. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  17. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  18. [self _initSubviews];
  19. }
  20. - (void)didReceiveMemoryWarning {
  21. [super didReceiveMemoryWarning];
  22. // Dispose of any resources that can be recreated.
  23. }
  24. - (void)viewWillAppear:(BOOL)animated
  25. {
  26. [super viewWillAppear:animated];
  27. self.navigationController.navigationBarHidden = YES;
  28. }
  29. - (void)dealloc
  30. {
  31. [[NSNotificationCenter defaultCenter] removeObserver:self];
  32. }
  33. #pragma mark - Getter
  34. - (UISearchBar *)searchBar
  35. {
  36. if (_searchBar == nil) {
  37. _searchBar = [[UISearchBar alloc] init];
  38. _searchBar.searchBarStyle = UISearchBarStyleMinimal;
  39. _searchBar.backgroundColor = [UIColor whiteColor];
  40. _searchBar.returnKeyType = UIReturnKeyDone;
  41. }
  42. return _searchBar;
  43. }
  44. #pragma mark - Subviews
  45. - (void)_initSubviews
  46. {
  47. self.view.backgroundColor = [UIColor whiteColor];
  48. [self.view addSubview:self.searchBar];
  49. [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.top.equalTo(self.view.mas_top).offset(25);
  51. make.left.equalTo(self.view.mas_left);
  52. make.right.equalTo(self.view.mas_right);
  53. make.height.equalTo(@50);
  54. }];
  55. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.equalTo(self.searchBar.mas_bottom);
  57. make.left.equalTo(self.view.mas_left);
  58. make.right.equalTo(self.view.mas_right);
  59. make.bottom.equalTo(self.view.mas_bottom);
  60. }];
  61. self.defaultFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80)];
  62. UILabel *label = [[UILabel alloc] init];
  63. label.text = NSLocalizedString(@"title.searchNoResult", @"Search has no result");
  64. label.textAlignment = NSTextAlignmentCenter;
  65. label.textColor = [UIColor grayColor];
  66. label.font = [UIFont systemFontOfSize:17.0];
  67. [self.defaultFooterView addSubview:label];
  68. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.top.equalTo(self.defaultFooterView).offset(20);
  70. make.left.equalTo(self.defaultFooterView).offset(20);
  71. make.right.equalTo(self.defaultFooterView).offset(-20);
  72. make.bottom.equalTo(self.defaultFooterView).offset(-20);
  73. }];
  74. }
  75. #pragma mark - Table view data source
  76. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  77. {
  78. if (_numberOfSectionsInTableViewCompletion) {
  79. return _numberOfSectionsInTableViewCompletion(tableView);
  80. }
  81. // Return the number of sections.
  82. return 1;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  85. {
  86. if (_numberOfRowsInSectionCompletion) {
  87. return _numberOfRowsInSectionCompletion(tableView, section);
  88. }
  89. // Return the number of rows in the section.
  90. return [self.dataArray count];
  91. }
  92. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94. if (_cellForRowAtIndexPathCompletion) {
  95. return _cellForRowAtIndexPathCompletion(tableView, indexPath);
  96. }
  97. else{
  98. static NSString *CellIdentifier = @"UITableViewCell";
  99. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  100. // Configure the cell...
  101. if (cell == nil || !cell) {
  102. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  103. }
  104. return cell;
  105. }
  106. }
  107. // Override to support conditional editing of the table view.
  108. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. // Return NO if you do not want the specified item to be editable.
  111. if (_canEditRowAtIndexPath) {
  112. return _canEditRowAtIndexPath(tableView, indexPath);
  113. }
  114. else{
  115. return NO;
  116. }
  117. }
  118. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. //在iOS8.0上,必须加上这个方法才能出发左划操作
  121. if (_commitEditingAtIndexPath) {
  122. return _commitEditingAtIndexPath(tableView, editingStyle, indexPath);
  123. }
  124. }
  125. #pragma mark - Table view delegate
  126. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. if (_didSelectRowAtIndexPathCompletion) {
  129. return _didSelectRowAtIndexPathCompletion(tableView, indexPath);
  130. }
  131. }
  132. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  133. {
  134. if (_didDeselectRowAtIndexPathCompletion) {
  135. _didDeselectRowAtIndexPathCompletion(tableView, indexPath);
  136. }
  137. }
  138. #pragma mark - KeyBoard
  139. - (void)keyBoardWillShow:(NSNotification *)note
  140. {
  141. // 获取用户信息
  142. NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
  143. // 获取键盘高度
  144. CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  145. CGFloat keyBoardHeight = keyBoardBounds.size.height;
  146. // 获取键盘动画时间
  147. CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  148. // 定义好动作
  149. void (^animation)(void) = ^void(void) {
  150. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  151. make.bottom.equalTo(self.view.mas_bottom).offset(-keyBoardHeight);
  152. }];
  153. };
  154. if (animationTime > 0) {
  155. [UIView animateWithDuration:animationTime animations:animation];
  156. } else {
  157. animation();
  158. }
  159. }
  160. - (void)keyBoardWillHide:(NSNotification *)note
  161. {
  162. // 获取用户信息
  163. NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
  164. // 获取键盘动画时间
  165. CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  166. // 定义好动作
  167. void (^animation)(void) = ^void(void) {
  168. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  169. make.bottom.equalTo(self.view.mas_bottom);
  170. }];
  171. };
  172. if (animationTime > 0) {
  173. [UIView animateWithDuration:animationTime animations:animation];
  174. } else {
  175. animation();
  176. }
  177. }
  178. #pragma mark - Data
  179. - (void)tableViewDidTriggerFooterRefresh
  180. {
  181. if (_footerBeginRefreshCompletion) {
  182. ++self.page;
  183. _footerBeginRefreshCompletion(self.tableView);
  184. }
  185. }
  186. @end