FamilyListViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // FamilyListViewController.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 16/9/26.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "FamilyListViewController.h"
  9. #import "FamilyListViewCell.h"
  10. #import "MJRefresh.h"
  11. #import "FamilyListModel.h"
  12. #import "FamilyDesViewController.h"
  13. @interface FamilyListViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,FamilyListViewCellDelegate>
  14. {
  15. NSString *_searchString;
  16. UIView *_searchHeaderView;
  17. UIButton *_rightButton;
  18. UIImageView *_searchImgView;
  19. }
  20. @property (nonatomic, strong) UISearchBar *SearchBar;
  21. @property (nonatomic, strong) UIView * displayView;
  22. @property (nonatomic, strong) UIView * slideLineView;
  23. @property (nonatomic, strong) NSMutableArray *userDataArray;
  24. @property (nonatomic, strong) UITableView *displayTabel;
  25. @property (nonatomic, assign) int has_next;
  26. @property (nonatomic, assign) int currentPage;;
  27. @property (nonatomic, assign) int state;;
  28. @property (nonatomic, strong) UITextField *textFiled;
  29. @end
  30. @implementation FamilyListViewController
  31. - (void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. UIApplication *application = [UIApplication sharedApplication];
  35. [application setStatusBarHidden:NO];
  36. [IQKeyboardManager sharedManager].enable = YES;
  37. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  38. _rightButton.hidden = NO;
  39. _textFiled.hidden = NO;
  40. _searchImgView.hidden = NO;
  41. _searchHeaderView.hidden = NO;
  42. [self loadNetDataWithPage:1];
  43. }
  44. - (void)viewDidLoad
  45. {
  46. [super viewDidLoad];
  47. self.view.backgroundColor = [UIColor whiteColor];
  48. _userDataArray = [[NSMutableArray alloc]initWithCapacity:0];
  49. [self searchbarDisplay];
  50. _currentPage = 1;
  51. [self creatTabelView];
  52. }
  53. - (void)searchbarDisplay
  54. {
  55. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  56. self.navigationItem.title = ASLocalizedString(@"公会列表");
  57. //画线
  58. UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 0.5)];
  59. lineLabel.backgroundColor = kAppGrayColor4;
  60. lineLabel.alpha = 0.5;
  61. [self.view addSubview:lineLabel];
  62. _searchHeaderView = [[UIView alloc] initWithFrame:CGRectMake(10, 6, kScreenW-80, 30)];
  63. [_searchHeaderView setBackgroundColor:[UIColor whiteColor]];
  64. _searchHeaderView.layer.cornerRadius = 5;
  65. _searchHeaderView.layer.borderWidth = 1;
  66. _searchHeaderView.layer.borderColor = kAppMainColor.CGColor;
  67. [self.view addSubview:_searchHeaderView];
  68. _searchImgView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 16, 16)];
  69. _searchImgView.backgroundColor = [UIColor whiteColor];
  70. _searchImgView.image = [UIImage imageNamed:@"ic_edit_search_gray"];
  71. [_searchHeaderView addSubview:_searchImgView];
  72. _textFiled = [[UITextField alloc]initWithFrame:CGRectMake(22, 0, kScreenW -110, 30)];
  73. _textFiled.backgroundColor = [UIColor whiteColor];
  74. _textFiled.textAlignment = NSTextAlignmentLeft;
  75. _textFiled.font = [UIFont systemFontOfSize:14];
  76. _textFiled.delegate = self;
  77. [_textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  78. _textFiled.clearsOnBeginEditing = YES;
  79. _textFiled.clearsContextBeforeDrawing = YES;
  80. _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
  81. _textFiled.placeholder = ASLocalizedString(@"请输入您想要加入的公会");
  82. [_searchHeaderView addSubview:_textFiled];
  83. _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW-70, 6, 60, 30)];
  84. [_rightButton setTitle:ASLocalizedString(@"取消")forState:0];
  85. [_rightButton setTitleColor:kAppMainColor forState:0];
  86. [_rightButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
  87. [self.view addSubview:_rightButton];
  88. self.view.backgroundColor = [UIColor whiteColor];
  89. UIView * blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kScreenW, 10)];
  90. blackView.backgroundColor = kFamilyBackGroundColor;
  91. blackView.alpha = 0.5;
  92. [self.view addSubview:blackView];
  93. }
  94. - (void)cancelButtonAction
  95. {
  96. [_textFiled resignFirstResponder];
  97. [self.navigationController popViewControllerAnimated:NO];
  98. }
  99. - (void)comeBack
  100. {
  101. [_textFiled resignFirstResponder];
  102. [self.navigationController popViewControllerAnimated:NO];
  103. }
  104. - (void)viewWillDisappear:(BOOL)animated
  105. {
  106. [super viewWillDisappear:animated];
  107. _rightButton.hidden = YES;
  108. _textFiled.hidden = YES;
  109. _searchImgView.hidden = YES;
  110. _searchHeaderView.hidden = YES;
  111. [IQKeyboardManager sharedManager].enable = NO;
  112. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  113. }
  114. #pragma mark展示数据的表格创建
  115. - (void)creatTabelView
  116. {
  117. _displayTabel = [[UITableView alloc] initWithFrame:CGRectMake(0,50,kScreenW, kScreenH-110) style:UITableViewStylePlain];
  118. _displayTabel.delegate =self;
  119. _displayTabel.dataSource =self;
  120. _displayTabel.separatorStyle = UITableViewCellSeparatorStyleNone;
  121. [self.view addSubview:_displayTabel];
  122. [BGMJRefreshManager refresh:_displayTabel target:self headerRereshAction:@selector(headerReresh) footerRereshAction:@selector(footerReresh)];
  123. }
  124. - (void)headerReresh
  125. {
  126. [self loadNetDataWithPage:1];
  127. }
  128. - (void)footerReresh
  129. {
  130. if (_has_next == 1)
  131. {
  132. _currentPage ++;
  133. [self loadNetDataWithPage:_currentPage];
  134. }
  135. else
  136. {
  137. [BGMJRefreshManager endRefresh:_displayTabel];
  138. }
  139. }
  140. #pragma mark 请求数据
  141. - (void)loadNetDataWithPage:(int)page
  142. {
  143. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  144. [parmDict setObject:@"family" forKey:@"ctl"];
  145. [parmDict setObject:@"family_list" forKey:@"act"];
  146. if (_textFiled.text.length)
  147. {
  148. [parmDict setObject:_textFiled.text forKey:@"family_name"];
  149. }
  150. else
  151. {
  152. [parmDict setObject:@"" forKey:@"family_name"];
  153. }
  154. [parmDict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
  155. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  156. {
  157. _state = [responseJson toInt:@"status"];
  158. if (_state == 1)
  159. {
  160. NSDictionary * dic = [responseJson objectForKey:@"page"];
  161. if (dic && [dic isKindOfClass:[NSDictionary class]])
  162. {
  163. _currentPage = [dic toInt:@"page"];
  164. if (_currentPage == 1 || _currentPage == 0)
  165. {
  166. [_userDataArray removeAllObjects];
  167. }
  168. _has_next = [dic toInt:@"has_next"];
  169. }
  170. NSArray *listArray = [responseJson objectForKey:@"list"];
  171. if (listArray && [listArray isKindOfClass:[NSArray class]]&& listArray.count)
  172. {
  173. for (NSDictionary *dict in listArray)
  174. {
  175. FamilyListModel * model = [FamilyListModel mj_objectWithKeyValues:dict];
  176. [_userDataArray addObject:model];
  177. }
  178. }
  179. }
  180. [_displayTabel reloadData];
  181. [BGMJRefreshManager endRefresh:_displayTabel];
  182. } FailureBlock:^(NSError *error)
  183. {
  184. [BGMJRefreshManager endRefresh:_displayTabel];
  185. }];
  186. }
  187. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  188. {
  189. return 1;
  190. }
  191. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  192. {
  193. if (_userDataArray.count>0) {
  194. return _userDataArray.count;
  195. }
  196. else
  197. return 0;
  198. }
  199. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  200. {
  201. return 60;
  202. }
  203. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. static NSString * cellID = @"cellID";
  206. FamilyListViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  207. if (cell== nil) {
  208. cell = [[FamilyListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  209. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  210. }
  211. cell.model = _userDataArray[indexPath.row];
  212. cell.delegate = self;
  213. return cell;
  214. }
  215. //点击cell跳转到公会详情
  216. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  217. {
  218. FamilyListModel * model = _userDataArray[indexPath.row];
  219. FamilyDesViewController * familyDesVC = [[FamilyDesViewController alloc] init];
  220. familyDesVC.isFamilyHeder = 2;
  221. familyDesVC.jid = model.family_id;
  222. familyDesVC.is_apply = model.is_apply;
  223. familyDesVC.listModel = model;
  224. // for (FamilyListModel * model in _userDataArray) {
  225. // if ([model.is_apply isEqualToString:@"2"]) {
  226. // }
  227. // }
  228. [self.navigationController pushViewController:familyDesVC animated:YES];
  229. }
  230. // 监听改变按钮
  231. - (void)textFieldDidChange:(UITextField*) sender
  232. {
  233. NSLog(@"_SearchBarField.text==%@",_textFiled.text);
  234. [self loadNetDataWithPage:1];
  235. }
  236. //当用户按下ruturn,把焦点从textField移开那么键盘就会消失了
  237. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  238. {
  239. [textField resignFirstResponder];
  240. // [self loadNetDataWithPage:1];
  241. return YES;
  242. }
  243. - (void)textFieldDidEndEditing:(UITextField *)textField
  244. {
  245. [self loadNetDataWithPage:1];
  246. }
  247. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  248. {
  249. // [_textFiled becomeFirstResponder];
  250. return YES;
  251. }
  252. //滑动键盘下落
  253. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  254. {
  255. [_textFiled resignFirstResponder];
  256. }
  257. //申请加入公会
  258. - (void)applyWithFamilyListViewCell:(FamilyListViewCell *)cell
  259. {
  260. for (FamilyListModel * model in _userDataArray)
  261. {
  262. if ([model.is_apply isEqualToString:@"2"])
  263. {
  264. return;
  265. }
  266. }
  267. FWWeakify(self)
  268. [FanweMessage alert:nil message:ASLocalizedString(@"是否申请加入该公会")destructiveAction:^{
  269. FWStrongify(self)
  270. NSIndexPath * indexPath = [_displayTabel indexPathForCell:cell];
  271. FamilyListModel * model = _userDataArray[indexPath.row];
  272. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  273. [parmDict setObject:@"family_user" forKey:@"ctl"];
  274. [parmDict setObject:@"user_join" forKey:@"act"];
  275. [parmDict setObject:model.family_id forKey:@"family_id"];
  276. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  277. if ([responseJson toInt:@"status"]==1)
  278. {
  279. model.is_apply = @"1";
  280. self.userDataArray[indexPath.row] = model;
  281. [self.displayTabel reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  282. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"申请已提交")];
  283. }
  284. } FailureBlock:^(NSError *error) {
  285. NSLog(@"%@",error);
  286. }];
  287. } cancelAction:^{
  288. }];
  289. }
  290. @end