SSearchVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // SSearchVC.m
  3. // BuguLive
  4. //
  5. // Created by 丁凯 on 2017/6/21.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "SSearchVC.h"
  9. #import "FollowerTableViewCell.h"
  10. #import "SenderModel.h"
  11. #import "BogoSearchListView.h"
  12. #import "BogoSearchNavTopView.h"
  13. @interface SSearchVC ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,loadAgainDelegate>
  14. @property (nonatomic, strong) UITextField *myTextFiled; //搜索TextField
  15. @property (nonatomic, strong) NSMutableArray *userDataArray;
  16. @property (nonatomic, strong) UITableView *displayTabel;
  17. @property (nonatomic, assign) int has_next;
  18. @property (nonatomic, assign) int currentPage;
  19. @property(nonatomic, strong) BogoSearchListView *searchListView;
  20. @property(nonatomic, strong) BogoSearchNavTopView *navView;
  21. @end
  22. @implementation SSearchVC
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = kWhiteColor;
  27. }
  28. - (void)initFWUI
  29. {
  30. [super initFWUI];
  31. [self.view addSubview:self.navView];
  32. [self.view addSubview:self.searchListView];
  33. _displayTabel = [[UITableView alloc]init];
  34. _displayTabel.frame = CGRectMake(0,self.navView.bottom,kScreenW, kScreenH-self.navView.bottom);
  35. _displayTabel.delegate =self;
  36. _displayTabel.dataSource =self;
  37. _displayTabel.separatorStyle = UITableViewCellSeparatorStyleNone;
  38. _displayTabel.backgroundColor = kWhiteColor;
  39. // [self showMyHud];
  40. [_displayTabel registerNib:[UINib nibWithNibName:@"FollowerTableViewCell" bundle:nil] forCellReuseIdentifier:@"FollowerTableViewCell"];
  41. [BGMJRefreshManager refresh:_displayTabel target:self headerRereshAction:@selector(headerReresh) footerRereshAction:@selector(footerReresh)];
  42. _displayTabel.hidden = YES;
  43. [self.view addSubview:_displayTabel];
  44. // [self searchbarDisplay];
  45. }
  46. - (void)initFWData
  47. {
  48. [super initFWData];
  49. }
  50. - (void)viewWillAppear:(BOOL)animated
  51. {
  52. [super viewWillAppear:animated];
  53. self.myTextFiled.hidden = NO;
  54. [IQKeyboardManager sharedManager].enable = YES;
  55. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  56. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  57. UIApplication *application = [UIApplication sharedApplication];
  58. [application setStatusBarHidden:NO];
  59. [self.navigationController setNavigationBarHidden:YES animated:animated];
  60. }
  61. - (void)viewWillDisappear:(BOOL)animated
  62. {
  63. [super viewWillDisappear:animated];
  64. self.myTextFiled.hidden = YES;
  65. [IQKeyboardManager sharedManager].enable = NO;
  66. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  67. }
  68. #pragma mark 搜索栏
  69. - (void)searchbarDisplay
  70. {
  71. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  72. // 右上角按钮
  73. UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, kNavigationBarHeight)];
  74. [rightButton setImage:[UIImage imageNamed:@"hm_search"] forState:UIControlStateNormal];
  75. [rightButton addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
  76. rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  77. UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
  78. self.navigationItem.rightBarButtonItem = rightButtonItem;
  79. // self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(searchBtn) image:@"hm_search" highImage:@"hm_search"];
  80. [self.navigationController.navigationBar addSubview:self.myTextFiled];
  81. }
  82. - (void)searchBtn
  83. {
  84. if (!self.myTextFiled.text.length)
  85. {
  86. [FanweMessage alertHUD:ASLocalizedString(@"请输入用户名或用户ID")];
  87. return;
  88. }else
  89. {
  90. [self.myTextFiled resignFirstResponder];
  91. [self loadNetDataWithPage:1 andTextFiledStr:self.myTextFiled.text];
  92. }
  93. }
  94. #pragma mark 头部刷新
  95. - (void)headerReresh
  96. {
  97. [self loadNetDataWithPage:1 andTextFiledStr:self.myTextFiled.text];
  98. }
  99. #pragma mark 尾部刷新
  100. - (void)footerReresh
  101. {
  102. if (_has_next == 1)
  103. {
  104. _currentPage ++;
  105. [self loadNetDataWithPage:_currentPage andTextFiledStr:self.myTextFiled.text];
  106. }
  107. else
  108. {
  109. [BGMJRefreshManager endRefresh:_displayTabel];
  110. }
  111. }
  112. #pragma mark 监听改变按钮
  113. - (void)textFieldDidChange:(UITextField *) sender
  114. {
  115. [self loadNetDataWithPage:_currentPage andTextFiledStr:sender.text];
  116. }
  117. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
  118. self.searchListView.hidden = YES;
  119. _displayTabel.hidden = NO;
  120. if (self.searchListView.listArr.count > 0) {
  121. [self hideNoContentView];
  122. }else{
  123. [self showNoContentView];
  124. }
  125. return YES;
  126. }
  127. - (void)textFieldDidEndEditing:(UITextField *)textField{
  128. if (textField.text.length < 1) {
  129. _displayTabel.hidden = YES;
  130. self.searchListView.hidden = NO;
  131. [self hideNoContentView];
  132. }
  133. }
  134. #pragma mark 返回
  135. - (void)comeBack
  136. {
  137. [self.myTextFiled resignFirstResponder];
  138. [[AppDelegate sharedAppDelegate]popViewController];
  139. }
  140. #pragma mark 请求数据
  141. - (void)loadNetDataWithPage:(int)page andTextFiledStr:(NSString *)TextFiledStr
  142. {
  143. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  144. [parmDict setObject:@"user" forKey:@"ctl"];
  145. [parmDict setObject:@"search" forKey:@"act"];
  146. if(TextFiledStr.length > 0)
  147. {
  148. [parmDict setObject:TextFiledStr forKey:@"keyword"];
  149. }
  150. [parmDict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"p"];
  151. FWWeakify(self)
  152. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  153. {
  154. FWStrongify(self)
  155. [self hideMyHud];
  156. if (responseJson)
  157. {
  158. if (responseJson.count)
  159. {
  160. if ([responseJson toInt:@"status"] == 1)
  161. {
  162. _currentPage = [responseJson toInt:@"page"];
  163. if (_currentPage == 1 || _currentPage == 0)
  164. {
  165. [self.userDataArray removeAllObjects];
  166. }
  167. _has_next = [responseJson toInt:@"has_next"];
  168. NSArray *listArray = [responseJson objectForKey:@"list"];
  169. if (listArray && [listArray isKindOfClass:[NSArray class]])
  170. {
  171. if (listArray.count)
  172. {
  173. for (NSDictionary *dict in listArray)
  174. {
  175. SenderModel *sModel = [SenderModel mj_objectWithKeyValues:dict];
  176. [_userDataArray addObject:sModel];
  177. }
  178. }
  179. }
  180. [_displayTabel reloadData];
  181. if (!self.displayTabel.hidden) {
  182. if (_userDataArray.count > 0)
  183. {
  184. [self hideNoContentView];
  185. }else
  186. {
  187. [self showNoContentView];
  188. }
  189. }
  190. }
  191. }
  192. }
  193. [BGMJRefreshManager endRefresh:_displayTabel];
  194. } FailureBlock:^(NSError *error){
  195. FWStrongify(self)
  196. [self hideMyHud];
  197. [BGMJRefreshManager endRefresh:self.displayTabel];
  198. }];
  199. }
  200. #pragma mark ----tabelView代理方法
  201. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  202. {
  203. return _userDataArray.count;
  204. }
  205. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  206. {
  207. return 1;
  208. }
  209. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  210. {
  211. return 65*kAppRowHScale;
  212. }
  213. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  214. {
  215. FollowerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FollowerTableViewCell" forIndexPath:indexPath];
  216. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  217. cell.delegate = self;
  218. if (_userDataArray.count > 0 && indexPath.section < _userDataArray.count)
  219. {
  220. cell.hidden = NO;
  221. SenderModel *model = _userDataArray[indexPath.section];
  222. [cell creatCellWithModel:model WithRow:(int)indexPath.section];
  223. }else
  224. {
  225. cell.hidden = YES;
  226. }
  227. return cell;
  228. }
  229. #pragma mark 点击头像重新加载某一段
  230. - (void)loadAgainSection:(int)section withHasFonce:(int)hasFonce
  231. {
  232. SenderModel *model = _userDataArray[section];
  233. if (hasFonce == 1)
  234. {
  235. model.follow_id = @"1";
  236. }
  237. else
  238. {
  239. model.follow_id = @"0";
  240. }
  241. _userDataArray[section] = model;
  242. [_displayTabel reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
  243. }
  244. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  245. {
  246. if (_userDataArray.count > 0 && indexPath.section < _userDataArray.count)
  247. {
  248. SenderModel *sModel = _userDataArray[indexPath.section];
  249. SHomePageVC *homeVC = [[SHomePageVC alloc]init];
  250. homeVC.user_id = sModel.user_id;
  251. homeVC.type = 0;
  252. [[AppDelegate sharedAppDelegate]pushViewController:homeVC animated:YES];
  253. }
  254. }
  255. #pragma mark ------------------------------------getter方法------------------------------------
  256. - (UITextField *)myTextFiled
  257. {
  258. if (!_myTextFiled)
  259. {
  260. _myTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(50*kScaleWidth, (44-25*kScaleWidth)/2, kScreenW -100*kScaleWidth, 25*kScaleWidth)];
  261. _myTextFiled.backgroundColor = kBackGroundColor;
  262. _myTextFiled.layer.cornerRadius = 25*kScaleWidth/2;
  263. _myTextFiled.textAlignment = NSTextAlignmentCenter;
  264. _myTextFiled.font = [UIFont systemFontOfSize:12];
  265. _myTextFiled.delegate = self;
  266. [_myTextFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  267. _myTextFiled.clearsOnBeginEditing = NO;
  268. // _myTextFiled.clearsContextBeforeDrawing = YES;
  269. _myTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
  270. // _myTextFiled.leftViewMode = UITextFieldViewModeAlways;//设置左边距显示的时机,这个表示一直显示
  271. _myTextFiled.placeholder = ASLocalizedString(@"请搜索用户名或用户ID");
  272. }
  273. return _myTextFiled;
  274. }
  275. - (NSMutableArray *)userDataArray
  276. {
  277. if (!_userDataArray)
  278. {
  279. _userDataArray = [[NSMutableArray alloc]init];
  280. }
  281. return _userDataArray;
  282. }
  283. -(BogoSearchListView *)searchListView{
  284. if (!_searchListView) {
  285. _searchListView = [[BogoSearchListView alloc]initWithFrame:CGRectMake(0, self.navView.bottom, kScreenW, kScreenH - self.navView.bottom - MG_BOTTOM_MARGIN)];
  286. _searchListView.backgroundColor = kWhiteColor;
  287. }
  288. return _searchListView;
  289. }
  290. -(BogoSearchNavTopView *)navView{
  291. if (!_navView) {
  292. _navView = [[BogoSearchNavTopView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight, kScreenW, kRealValue(40))];
  293. _navView.searchField.delegate = self;
  294. [_navView.searchField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  295. }
  296. return _navView;
  297. }
  298. @end