JXSearchVC.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. //
  2. // JXSearchVC.m
  3. // shiku_im
  4. //
  5. // Created by IMAC on 2019/8/6.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "JXSearchVC.h"
  9. #import "JXRoomMember.h"
  10. #import "JXChatViewController.h"
  11. #import "JXSearchListVC.h"
  12. #import "JXSearchShowCell.h"
  13. #import "JXSearchMoreCell.h"
  14. #import "JXSearchRecordCell.h"
  15. #import "JXClearAllRecordCell.h"
  16. #import "JXXMPP.h"
  17. #import "JXRoomPool.h"
  18. #import "JXChatViewC.h"
  19. @interface JXSearchVC ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,JXSearchListDelegate, JXSearchRecordCellDelegate>
  20. {
  21. CGRect bounds;
  22. CGFloat cornerRadius;
  23. }
  24. @property (nonatomic,strong)UIView *baseView;
  25. @property (nonatomic,strong)UITableView *searchRecordView;
  26. @property (nonatomic,strong)UITableView *tableview;
  27. @property (nonatomic,strong)UISearchBar *searchbar;
  28. @property (nonatomic,strong)NSString *tableName;
  29. @property (nonatomic,strong)NSMutableArray *sectionArray;//tableView模块数组
  30. @property (nonatomic,strong)NSMutableDictionary *sectionDictonary;//tableView模块数据
  31. @property (nonatomic,strong)NSMutableArray *contactsArray;//联系人
  32. @property (nonatomic,strong)NSMutableArray *groupArray;//群组
  33. @property (nonatomic,strong)NSMutableDictionary *groupDictionary;
  34. @property (nonatomic,strong)NSMutableArray *publicArray;//公众号
  35. @property (nonatomic,strong)NSMutableArray *recordArray;//聊天记录
  36. @property (nonatomic,strong)NSMutableDictionary *msgDictionary;//消息字典
  37. @property (nonatomic,strong)NSMutableArray *searchRecordArray;
  38. //@property (nonatomic,strong)UIButton *cancelBtn;
  39. @property (nonatomic,strong)UIButton *backbtn;
  40. @property (nonatomic,strong)JXSearchListVC *searchListVC;
  41. @property (nonatomic,assign)BOOL isShow;
  42. @end
  43. @implementation JXSearchVC
  44. - (instancetype)initWithTable:(NSString *)table{
  45. self = [super init];
  46. if (self) {
  47. self.sectionArray = [NSMutableArray array];
  48. self.sectionDictonary = [NSMutableDictionary dictionary];
  49. self.contactsArray = [NSMutableArray array];
  50. self.groupArray = [NSMutableArray array];
  51. self.groupDictionary = [NSMutableDictionary dictionary];
  52. self.publicArray = [NSMutableArray array];
  53. self.recordArray = [NSMutableArray array];
  54. self.msgDictionary = [NSMutableDictionary dictionary];
  55. self.tableName = table;
  56. self.searchRecordArray = [[JXUserObject sharedInstance] fetchAllSearchRecordWithTable:self.tableName];
  57. _isShow = NO;
  58. }
  59. return self;
  60. }
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. [self creatView];
  64. cornerRadius = 7;
  65. }
  66. - (void)viewDidAppear:(BOOL)animated{
  67. [super viewDidAppear:animated];
  68. if (!_isShow) {
  69. [self.searchbar becomeFirstResponder];
  70. _isShow = YES;
  71. }
  72. }
  73. #pragma mark -- 界面控件
  74. - (void)creatView{
  75. UIView *stastusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, SCREEN_BAR)];
  76. if (THESIMPLESTYLE) {
  77. stastusBarView.backgroundColor = [UIColor whiteColor];
  78. }else{
  79. stastusBarView.backgroundColor = [g_theme themeColor];
  80. }
  81. [self.view addSubview:stastusBarView];
  82. //基础view
  83. self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_BAR, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_BAR)];
  84. self.baseView.backgroundColor = [UIColor whiteColor];
  85. [self.view addSubview:self.baseView];
  86. //导航栏
  87. UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 44)];
  88. if (THESIMPLESTYLE) {
  89. bar.barTintColor = [UIColor whiteColor];
  90. }else{
  91. bar.barTintColor = [g_theme themeColor];
  92. }
  93. bar.translucent = NO;
  94. //搜索框
  95. // self.searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 5, JX_SCREEN_WIDTH-20, 33)];
  96. self.searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(40, 5, JX_SCREEN_WIDTH-55, 33)];
  97. self.searchbar.delegate = self;
  98. self.searchbar.placeholder = SEARCH;
  99. // self.searchbar.showsCancelButton = YES;
  100. //搜索框样式
  101. NSString *version = [UIDevice currentDevice].systemVersion;
  102. UITextField *searchField;
  103. if (version.doubleValue >= 13.0) {
  104. // 这里是对 13.0 以上的iOS系统进行处理
  105. NSUInteger Views = [self.searchbar.subviews count];
  106. for(int i = 0; i < Views; i++) {
  107. if([[self.searchbar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
  108. searchField = [self.searchbar.subviews objectAtIndex:i];
  109. }
  110. }
  111. //这里设置相关属性
  112. } else {
  113. // 这里是对 13.0 以下的iOS系统进行处理
  114. searchField = [self.searchbar valueForKey:@"_searchField"];
  115.  //这里设置相关属性
  116.       }
  117. // UITextField *seachTextFild = [self.searchbar valueForKey:@"_searchField"];
  118. searchField.tintColor = [g_theme themeColor];
  119. searchField.backgroundColor = HEXCOLOR(0xf2f2f2);
  120. UIView *backView = [self subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView" toView:self.searchbar];
  121. backView.layer.cornerRadius = 5;
  122. backView.layer.masksToBounds = YES;
  123. backView.backgroundColor = HEXCOLOR(0xf2f2f2);
  124. // //自定义取消按钮样式
  125. // self.cancelBtn = [self.searchbar valueForKey:@"_cancelButton"];
  126. // [self.cancelBtn setTitle:CANCEL forState:UIControlStateNormal];
  127. // if (THESIMPLESTYLE) {
  128. // [self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  129. // }else{
  130. // [self.cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  131. // }
  132. [bar addSubview:self.searchbar];
  133. self.backbtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 11, 20, 20)];
  134. if (THESIMPLESTYLE) {
  135. [self.backbtn setBackgroundImage:[UIImage imageNamed:@"title_back_black"] forState:UIControlStateNormal];
  136. }else{
  137. [self.backbtn setBackgroundImage:[UIImage imageNamed:@"title_back"] forState:UIControlStateNormal];
  138. }
  139. [self.backbtn addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
  140. [bar addSubview:self.backbtn];
  141. [self.baseView addSubview:bar];
  142. //关键字查询信息的tableView
  143. self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW)];
  144. self.tableview.tag = 1;
  145. self.tableview.dataSource = self;
  146. self.tableview.delegate = self;
  147. self.tableview.backgroundColor = HEXCOLOR(0xf2f2f2);
  148. UIView *tableHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 8)];
  149. tableHead.backgroundColor = HEXCOLOR(0xf2f2f2);
  150. self.tableview.tableHeaderView = tableHead;
  151. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 8)];
  152. view.backgroundColor = HEXCOLOR(0xf2f2f2);
  153. self.tableview.tableFooterView = view;
  154. self.tableview.separatorColor = HEXCOLOR(0xd9d9d9);
  155. [self.baseView addSubview:self.tableview];
  156. //指定内容查找
  157. self.searchRecordView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW)];
  158. self.searchRecordView.tag = 2;
  159. self.searchRecordView.delegate = self;
  160. self.searchRecordView.dataSource = self;
  161. self.searchRecordView.backgroundColor = HEXCOLOR(0xf2f2f2);
  162. self.searchRecordView.separatorColor = HEXCOLOR(0xd9d9d9);
  163. UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 8)];
  164. view2.backgroundColor = HEXCOLOR(0xf2f2f2);
  165. self.searchRecordView.tableFooterView = view2;
  166. [self.baseView addSubview:self.searchRecordView];
  167. }
  168. - (UIView*)subViewOfClassName:(NSString*)className toView:(UIView *)view{
  169. for (UIView* subView in view.subviews) {
  170. if ([NSStringFromClass(subView.class) isEqualToString:className]) {
  171. return subView;
  172. }
  173. UIView* resultFound = [self subViewOfClassName:className toView:subView];
  174. if (resultFound) {
  175. return resultFound;
  176. }
  177. }
  178. return nil;
  179. }
  180. #pragma mark -- 数据的计算
  181. //tavleview模块的行数
  182. - (NSInteger)displayCount:(NSMutableDictionary *)dic withArray:(NSMutableArray *)array withNumber:(NSInteger )i{
  183. if ([self arrayFromDictionary:dic forKey:array[i]].count == 0) {
  184. return 0;
  185. }
  186. if ([self arrayFromDictionary:dic forKey:array[i]].count != 0 && [self arrayFromDictionary:dic forKey:array[i]].count < 4) {
  187. return [self arrayFromDictionary:dic forKey:array[i]].count +1;
  188. }
  189. return 5;
  190. }
  191. //
  192. - (NSMutableArray *)arrayFromDictionary:(NSMutableDictionary *)dic forKey:(NSString *)key{
  193. NSMutableArray *array = [dic objectForKey:key];
  194. return array;
  195. }
  196. //读取某用户或群组对象
  197. - (JXUserObject *)readFromDictionary:(NSMutableDictionary *)dic withArray:(NSMutableArray *)array withNumber1:(NSInteger )i number2:(NSInteger )j{
  198. NSMutableArray* array1 = [dic objectForKey:array[i]];
  199. JXUserObject *user = array1[j];
  200. return user;
  201. }
  202. //添加显示的模块数和模块名
  203. - (void)displaySection{
  204. [self.sectionArray removeAllObjects];
  205. [self updateSectionWithContentArray:self.contactsArray withString:CONTACT];
  206. [self updateSectionWithContentArray:self.groupArray withString:GROUP];
  207. [self updateSectionWithContentArray:self.publicArray withString:PUBLIC];
  208. [self updateSectionWithContentArray:self.recordArray withString:RECORD];
  209. }
  210. //更新搜索到的数据
  211. - (void)updateSectionWithContentArray:(NSMutableArray *)array2 withString:(NSString *)str{
  212. if (array2.count !=0) {
  213. if (![self.sectionArray containsObject:str]) {
  214. [self.sectionArray addObject:str];
  215. }
  216. [self.sectionDictonary setObject:array2 forKey:str];
  217. }else{
  218. if ([self.sectionArray containsObject:str]) {
  219. [self.sectionArray removeObject:str];
  220. }
  221. if ([[self.sectionDictonary allKeys] containsObject:str]) {
  222. [self.sectionDictonary removeObjectForKey:str];
  223. }
  224. }
  225. }
  226. #pragma mark -- 获取数据
  227. //获取联系人数据
  228. - (void)getContactsWithText:(NSString *)text{
  229. NSMutableArray *array = [[JXUserObject sharedInstance] searchContacts:text];
  230. [self.contactsArray removeAllObjects];
  231. if (array.count != 0) {
  232. [self.contactsArray addObjectsFromArray:array];
  233. }
  234. }
  235. //获取群聊数据
  236. - (void)getGroupWithText:(NSString *)text{
  237. NSMutableArray *array1 = [[JXUserObject sharedInstance] searchGroup:text withContent:@"userNickname"];
  238. NSMutableArray *array2 = [[JXUserObject sharedInstance] searchGroupMember:text];
  239. [self.groupArray removeAllObjects];
  240. [self.groupDictionary removeAllObjects];
  241. if (array1.count != 0) {
  242. [self.groupArray addObjectsFromArray:array1];
  243. NSMutableArray *roomArray = [NSMutableArray array];
  244. for (JXUserObject *user2 in self.groupArray) {
  245. [roomArray addObject:user2.roomId];
  246. }
  247. for (memberData *member in array2) {
  248. JXUserObject *user = [[JXUserObject sharedInstance] groupForRoomId:member.roomId];
  249. if (![roomArray containsObject:user.roomId]) {
  250. [self.groupArray addObject:user];
  251. [self.groupDictionary setValue:member.userNickName forKey:user.roomId];
  252. }
  253. }
  254. }else{
  255. for (memberData *member in array2) {
  256. JXUserObject *user = [[JXUserObject sharedInstance] groupForRoomId:member.roomId];
  257. [self.groupArray addObject:user];
  258. [self.groupDictionary setValue:member.userNickName forKey:user.roomId];
  259. }
  260. }
  261. }
  262. //获取公众号数据
  263. - (void)getPublicWithText:(NSString *)text{
  264. NSMutableArray *array = [[JXUserObject sharedInstance] searchPublic:text];
  265. [self.publicArray removeAllObjects];
  266. if (array.count != 0) {
  267. [self.publicArray addObjectsFromArray:array];
  268. }
  269. }
  270. //获取聊天记录数据
  271. - (void)getChatRecordWithText:(NSString *)text{
  272. NSMutableArray *array = [NSMutableArray array];
  273. [self.recordArray removeAllObjects];
  274. self.msgDictionary = [[JXUserObject sharedInstance] searchChatRecordWithText:text];
  275. array = [[JXUserObject sharedInstance] getAllContactsAndGroup];
  276. for (JXUserObject *user in array) {
  277. if ([[self.msgDictionary allKeys] containsObject:user.userId] && ![self.recordArray containsObject:user]) {
  278. NSMutableArray *msgArr = [self.msgDictionary objectForKey:user.userId];
  279. if (msgArr.count > 0) {
  280. [self.recordArray addObject:user];
  281. }
  282. }
  283. }
  284. }
  285. - (NSInteger)getMsgCountWithUserId:(NSString *)userId{
  286. NSMutableArray *array = [self.msgDictionary objectForKey:userId];
  287. return array.count;
  288. }
  289. //判断哪个模块
  290. - (BOOL)compareSectionSring:(NSString *)str withIndexPath:(NSIndexPath *)indexPath{
  291. return [self arrayFromDictionary:self.sectionDictonary forKey:str].count != 0 && [self.sectionArray indexOfObject:str] == indexPath.section;
  292. }
  293. //返回标记搜索查找的字符串
  294. - (NSMutableAttributedString *)showSearchTextColor:(NSString *)str{
  295. NSRange range = [str rangeOfString:self.searchbar.text options:NSCaseInsensitiveSearch];
  296. NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
  297. if (range.location != NSNotFound) {
  298. [string addAttribute:NSForegroundColorAttributeName value:[g_theme themeColor] range:range];
  299. }
  300. return string;
  301. }
  302. #pragma mark -- searchBar的代理
  303. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
  304. }
  305. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  306. if (searchText.length == 0) {
  307. [self.searchRecordView reloadData];
  308. [self.baseView bringSubviewToFront:self.searchRecordView];
  309. }else{
  310. [self.baseView bringSubviewToFront:self.tableview];
  311. [self getContactsWithText:searchText];
  312. [self getGroupWithText:searchText];
  313. [self getPublicWithText:searchText];
  314. [self getChatRecordWithText:searchText];
  315. [self displaySection];
  316. if (self.sectionArray.count == 0) {
  317. self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
  318. }else{
  319. self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  320. }
  321. [self.tableview reloadData];
  322. }
  323. }
  324. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
  325. [g_navigation dismissViewController:self animated:YES];
  326. }
  327. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
  328. [self.searchbar becomeFirstResponder];
  329. }
  330. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  331. [self.searchbar resignFirstResponder];
  332. // [self.cancelBtn setEnabled:YES];
  333. }
  334. #pragma mark -- tableView的代理和数据源
  335. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  336. [self.searchbar resignFirstResponder];
  337. // [self.cancelBtn setEnabled:YES];
  338. }
  339. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  340. if (tableView.tag == 1) {
  341. if (indexPath.row == 0) {
  342. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sectionNameCell"];
  343. cell.textLabel.text = self.sectionArray[indexPath.section];
  344. cell.textLabel.textColor = HEXCOLOR(0x999999);
  345. cell.textLabel.font = [UIFont systemFontOfSize:14];
  346. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  347. cell.textLabel.center = cell.center;
  348. [cell setSeparatorInset:UIEdgeInsetsMake(0, 8, 0, 8)];
  349. cell.userInteractionEnabled = NO;
  350. return cell;
  351. }
  352. if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3) {
  353. JXSearchShowCell *cell = [[JXSearchShowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chatCell" withNewStyle:JXSearchShowCellStyleUser];
  354. JXUserObject *user = [self readFromDictionary:self.sectionDictonary withArray:self.sectionArray withNumber1:indexPath.section number2:indexPath.row-1];
  355. NSMutableAttributedString *nameAString = [self showSearchTextColor:user.userNickname];
  356. if ([self compareSectionSring:CONTACT withIndexPath:indexPath]) {
  357. if ([user.userNickname localizedCaseInsensitiveContainsString:self.searchbar.text]) {
  358. cell.aboveAttributedText = nameAString;
  359. }else{
  360. cell.aboveText = user.userNickname;
  361. NSMutableAttributedString *string1 = [self showSearchTextColor:user.userId];
  362. NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",USERNUMBER]];
  363. [string2 appendAttributedString:string1];
  364. cell.belowAttributedText = string2;
  365. }
  366. [g_server getHeadImageSmall:user.userId userName:user.userNickname imageView:cell.headImgView];
  367. }
  368. if ([self compareSectionSring:GROUP withIndexPath:indexPath]) {
  369. NSString *str = user.roomId;
  370. NSString *containString = [self.groupDictionary objectForKey:str];
  371. if (![containString localizedStandardContainsString:self.searchbar.text]) {
  372. cell.aboveAttributedText = nameAString;
  373. }else{
  374. cell.aboveText = user.userNickname;
  375. NSMutableAttributedString *string3 = [self showSearchTextColor:containString];
  376. NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@: ",CONTAIN]];
  377. [string4 appendAttributedString:string3];
  378. cell.belowAttributedText = string4;
  379. }
  380. [g_server getRoomHeadImageSmall:user.userId roomId:user.roomId imageView:cell.headImgView];
  381. }
  382. if ([self compareSectionSring:PUBLIC withIndexPath:indexPath]){
  383. cell.aboveAttributedText = nameAString;
  384. [g_server getHeadImageSmall:user.userId userName:user.userNickname imageView:cell.headImgView];
  385. }
  386. if ([self compareSectionSring:RECORD withIndexPath:indexPath]) {
  387. cell.aboveText = user.userNickname;
  388. NSInteger i = [self getMsgCountWithUserId:user.userId];
  389. NSString *str = [NSString stringWithFormat:@"%ld%@",(long)i,RECORDNUMBERS];
  390. cell.belowText = str;
  391. if (user.roomId) {
  392. [g_server getRoomHeadImageSmall:user.userId roomId:user.roomId imageView:cell.headImgView];
  393. }else{
  394. [g_server getHeadImageSmall:user.userId userName:user.userNickname imageView:cell.headImgView];
  395. }
  396. }
  397. if (indexPath.row == 3) {
  398. [cell setSeparatorInset:UIEdgeInsetsMake(0, 8, 0, 8)];
  399. }
  400. return cell;
  401. }
  402. if (indexPath.row == 4) {
  403. JXSearchMoreCell *cell = [[JXSearchMoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"moreSearchCell"];
  404. cell.imgName = @"search_icon";
  405. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  406. cell.moreName = [NSString stringWithFormat:@"%@%@",MORE,self.sectionArray[indexPath.section]];
  407. return cell;
  408. }
  409. }
  410. if (indexPath.row == 0) {
  411. JXClearAllRecordCell *cell = [[JXClearAllRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"historycell" withTitle:@"history"];
  412. cell.imageView.image = [UIImage imageNamed:@"the_historical_record"];
  413. cell.textLabel.text = Localized(@"JX_HistoryRecord");
  414. cell.textLabel.font = [UIFont systemFontOfSize:15];
  415. cell.textLabel.textColor = HEXCOLOR(0x999999);
  416. cell.userInteractionEnabled = NO;
  417. [cell setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 0)];
  418. return cell;
  419. }
  420. if (indexPath.row == _searchRecordArray.count + 1) {
  421. JXClearAllRecordCell *cell = [[JXClearAllRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"deleteallcell"];
  422. cell.textLabel.text = Localized(@"JX_ClearAllRecords");
  423. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  424. cell.textLabel.font = [UIFont systemFontOfSize:15];
  425. cell.textLabel.textColor = HEXCOLOR(0xda4434);
  426. return cell;
  427. }
  428. JXSearchRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchRecirdCell"];
  429. if (!cell) {
  430. cell = [[JXSearchRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchRecirdCell"];
  431. }
  432. cell.delegate = self;
  433. cell.textLabel.text = self.searchRecordArray[indexPath.row - 1];
  434. return cell;
  435. }
  436. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  437. if (tableView.tag == 1) {
  438. cell.backgroundColor = UIColor.clearColor;
  439. CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  440. CGMutablePathRef pathRef = CGPathCreateMutable();
  441. bounds = CGRectInset(cell.bounds, 8, 0);
  442. if (indexPath.row == 0) {
  443. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
  444. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
  445. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  446. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
  447. }
  448. if (indexPath.row > 0 && indexPath.row != [tableView numberOfRowsInSection:indexPath.section] - 1) {
  449. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  450. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), 0);
  451. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), 0);
  452. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  453. }
  454. if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
  455. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  456. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
  457. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  458. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  459. if ([cell isKindOfClass:[JXSearchShowCell class]]) {
  460. [((JXSearchShowCell *)cell) cutSelectedView];
  461. }
  462. }
  463. layer.path = pathRef;
  464. CFRelease(pathRef);
  465. layer.fillColor = [UIColor whiteColor].CGColor;
  466. UIView *backView = [[UIView alloc] initWithFrame:bounds];
  467. [backView.layer addSublayer:layer];
  468. backView.backgroundColor = [UIColor clearColor];
  469. cell.backgroundView = backView;
  470. }
  471. }
  472. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  473. if (tableView.tag == 1) {
  474. return self.sectionArray.count;
  475. }
  476. return 1;
  477. }
  478. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  479. if (tableView.tag == 1) {
  480. return [self displayCount:self.sectionDictonary withArray:self.sectionArray withNumber:section];
  481. }
  482. if (self.searchRecordArray.count == 0) {
  483. return 0;
  484. }
  485. return self.searchRecordArray.count + 2;
  486. }
  487. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  488. if (tableView.tag == 1) {
  489. if (indexPath.row == 0) {
  490. return 44;
  491. }
  492. if (indexPath.row == 4) {
  493. return 50;
  494. }
  495. return 60;
  496. }
  497. // if (indexPath.row == 0) {
  498. // return 30;
  499. // }
  500. // if (indexPath.row == _searchRecordArray.count + 1) {
  501. // return 30;
  502. // }
  503. return 50;
  504. }
  505. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  506. if (tableView.tag == 1) {
  507. if (section == _sectionArray.count - 1) {
  508. return 0;
  509. }
  510. return 10;
  511. }
  512. return 0;
  513. }
  514. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  515. if (tableView.tag == 1) {
  516. if (section == _sectionArray.count - 1) {
  517. return nil;
  518. }
  519. UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableHeaderFooter"];
  520. if (!footer) {
  521. footer = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"tableHeaderFooter"];
  522. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 10)];
  523. view.backgroundColor = HEXCOLOR(0xf2f2f2);
  524. [footer addSubview:view];
  525. }
  526. return footer;
  527. }
  528. return nil;
  529. }
  530. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  531. if (tableView.tag == 1) {
  532. if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3){
  533. if ([self compareSectionSring:CONTACT withIndexPath:indexPath] || [self compareSectionSring:PUBLIC withIndexPath:indexPath]){
  534. JXUserObject *user = [self readFromDictionary:self.sectionDictonary withArray:self.sectionArray withNumber1:indexPath.section number2:indexPath.row-1];
  535. JXChatViewC *chatView = [[JXChatViewC alloc] init];
  536. chatView.chatPerson = user;
  537. [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
  538. [g_navigation pushViewController:chatView animated:YES];
  539. }
  540. if ([self compareSectionSring:GROUP withIndexPath:indexPath]) {
  541. JXUserObject *user = [self readFromDictionary:self.sectionDictonary withArray:self.sectionArray withNumber1:indexPath.section number2:indexPath.row-1];
  542. roomData * roomdata = [[roomData alloc] init];
  543. [roomdata getDataFromDict:[user toDictionary]];
  544. JXChatViewC *chatView = [JXChatViewC alloc];
  545. if ([user.groupStatus intValue] == 0) {
  546. chatView.chatRoom = [[JXXMPP sharedInstance].roomPool joinRoom:user.userId title:user.userNickname lastDate:nil isNew:NO];
  547. }
  548. chatView.title = user.userNickname;
  549. chatView.roomJid = user.userId;
  550. chatView.roomId = user.roomId;
  551. chatView.groupStatus = user.groupStatus;
  552. chatView.room = roomdata;
  553. chatView.chatPerson = user;
  554. chatView = [chatView init];
  555. [g_navigation pushViewController:chatView animated:YES];
  556. [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
  557. }
  558. if ([self compareSectionSring:RECORD withIndexPath:indexPath]) {
  559. self.searchListVC = [[JXSearchListVC alloc] initWithLable:RECORD withSearchText:self.searchbar.text withUserArray:nil withGroupDictionary:nil withMsgDictionary:self.msgDictionary isChatRecord:YES enterChatRecord:YES withPeople:self.recordArray[indexPath.row-1]];
  560. [self showListView:indexPath];
  561. }
  562. // [self.cancelBtn setEnabled:YES];
  563. }
  564. if (indexPath.row == 4) {
  565. if ([self compareSectionSring:CONTACT withIndexPath:indexPath]) {
  566. self.searchListVC = [[JXSearchListVC alloc] initWithLable:CONTACT withSearchText:self.searchbar.text withUserArray:self.contactsArray withGroupDictionary:nil withMsgDictionary:nil isChatRecord:NO enterChatRecord:NO withPeople:nil];
  567. [self showListView:indexPath];
  568. }
  569. if ([self compareSectionSring:GROUP withIndexPath:indexPath]) {
  570. self.searchListVC = [[JXSearchListVC alloc] initWithLable:GROUP withSearchText:self.searchbar.text withUserArray:self.groupArray withGroupDictionary:self.groupDictionary withMsgDictionary:nil isChatRecord:NO enterChatRecord:NO withPeople:nil];
  571. [self showListView:indexPath];
  572. }
  573. if ([self compareSectionSring:PUBLIC withIndexPath:indexPath]) {
  574. self.searchListVC = [[JXSearchListVC alloc] initWithLable:PUBLIC withSearchText:self.searchbar.text withUserArray:self.publicArray withGroupDictionary:nil withMsgDictionary:nil isChatRecord:NO enterChatRecord:NO withPeople:nil];
  575. [self showListView:indexPath];
  576. }
  577. if ([self compareSectionSring:RECORD withIndexPath:indexPath]) {
  578. self.searchListVC = [[JXSearchListVC alloc] initWithLable:RECORD withSearchText:self.searchbar.text withUserArray:self.recordArray withGroupDictionary:nil withMsgDictionary:self.msgDictionary isChatRecord:YES enterChatRecord:NO withPeople:nil];
  579. [self showListView:indexPath];
  580. }
  581. [self.searchbar resignFirstResponder];
  582. // [self.cancelBtn setEnabled:YES];
  583. }
  584. BOOL saveSuccess = [[JXUserObject sharedInstance] insertSearchRecord:self.searchbar.text withTable:self.tableName];
  585. if (!saveSuccess) {
  586. NSLog(@"保存搜索记录失败");
  587. }
  588. [_searchRecordArray removeAllObjects];
  589. _searchRecordArray = [[JXUserObject sharedInstance] fetchAllSearchRecordWithTable:self.tableName];
  590. }
  591. if (tableView.tag == 2) {
  592. if (indexPath.row == _searchRecordArray.count + 1) {
  593. UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"" message:Localized(@"JX_WhetherToClearTheSearchHistory") preferredStyle:UIAlertControllerStyleAlert];
  594. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:Localized(@"JX_Cencal") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  595. }];
  596. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:Localized(@"JX_Confirm") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  597. [_searchRecordArray removeAllObjects];
  598. [[JXUserObject sharedInstance] deleteAllSearchRecordWithTable:self.tableName];
  599. [_searchRecordView reloadData];
  600. }];
  601. [alertCon addAction:sureAction];
  602. [alertCon addAction:cancelAction];
  603. [self presentViewController:alertCon animated:YES completion:nil];
  604. }else{
  605. self.searchbar.text = _searchRecordArray[indexPath.row - 1];
  606. [self searchBar:self.searchbar textDidChange:self.searchbar.text];
  607. }
  608. [self.searchRecordView deselectRowAtIndexPath:indexPath animated:YES];
  609. }
  610. }
  611. - (void)showListView:(NSIndexPath *)indexPath{
  612. self.searchListVC.view.backgroundColor = [UIColor clearColor];
  613. [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
  614. self.searchListVC.delegate = self;
  615. [self addChildViewController:self.searchListVC];
  616. [self.view addSubview:self.searchListVC.view];
  617. [self.searchbar resignFirstResponder];
  618. // [self.cancelBtn setEnabled:YES];
  619. }
  620. - (void)deleteCell:(JXSearchRecordCell *)cell{
  621. NSString *msg = cell.textLabel.text;
  622. [[JXUserObject sharedInstance] deleteOneSearchRecord:msg withTable:self.tableName];
  623. [_searchRecordArray removeAllObjects];
  624. _searchRecordArray = [[JXUserObject sharedInstance] fetchAllSearchRecordWithTable:self.tableName];
  625. [self.searchRecordView reloadData];
  626. }
  627. - (void)dismissView{
  628. [g_navigation dismissViewController:self animated:YES];
  629. }
  630. #pragma mark -- ListView代理
  631. - (void)saveSearchRecord:(NSString *)searchRecord{
  632. BOOL saveSuccess = [[JXUserObject sharedInstance] insertSearchRecord:searchRecord withTable:self.tableName];
  633. if (!saveSuccess) {
  634. NSLog(@"保存搜索记录失败");
  635. }
  636. [_searchRecordArray removeAllObjects];
  637. _searchRecordArray = [[JXUserObject sharedInstance] fetchAllSearchRecordWithTable:self.tableName];
  638. }
  639. - (void)tapSearchCancelBtn{
  640. [self.searchListVC removeFromParentViewController];
  641. [self.searchListVC.view removeFromSuperview];
  642. [g_navigation dismissViewController:self animated:YES];
  643. }
  644. - (void)tapBackBtn:(BOOL )hasRecord{
  645. if (hasRecord) {
  646. // self.searchListVC.backbtn.frame = CGRectMake(-20, 11, 20, 20);
  647. // self.searchListVC.searchbar.frame = CGRectMake(10, 5, JX_SCREEN_WIDTH-20, 33);
  648. [UIView animateWithDuration:0.3 animations:^{
  649. self.searchListVC.recordtableview.frame = CGRectMake(JX_SCREEN_WIDTH, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW);
  650. } completion:^(BOOL finished) {
  651. [self.searchListVC removeFromParentViewController];
  652. [self.searchListVC.view removeFromSuperview];
  653. }];
  654. }else{
  655. // self.searchListVC.backbtn.frame = CGRectMake(-20, 11, 20, 20);
  656. // self.searchListVC.searchbar.frame = CGRectMake(10, 5, JX_SCREEN_WIDTH-20, 33);
  657. [UIView animateWithDuration:0.3 animations:^{
  658. self.searchListVC.tableview.frame = CGRectMake(JX_SCREEN_WIDTH, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW);
  659. } completion:^(BOOL finished) {
  660. [self.searchListVC removeFromParentViewController];
  661. [self.searchListVC.view removeFromSuperview];
  662. }];
  663. }
  664. }
  665. - (void)listDidAppear:(BOOL )hasRecord{
  666. if (hasRecord) {
  667. // self.searchListVC.searchbar.frame = CGRectMake(40, 5, JX_SCREEN_WIDTH-50, 33);
  668. // self.searchListVC.backbtn.frame = CGRectMake(10, 11, 20, 20);
  669. [UIView animateWithDuration:0.3 animations:^{
  670. self.searchListVC.recordtableview.frame = CGRectMake(0, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW);
  671. }];
  672. }else{
  673. // self.searchListVC.searchbar.frame = CGRectMake(40, 5, JX_SCREEN_WIDTH-50, 33);
  674. // self.searchListVC.backbtn.frame = CGRectMake(10, 11, 20, 20);
  675. [UIView animateWithDuration:0.3 animations:^{
  676. self.searchListVC.tableview.frame = CGRectMake(0, 44, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-SCREEN_VIEW);
  677. }];
  678. }
  679. }
  680. - (void)listWillDisappear:(BOOL)hasRecord {
  681. }
  682. @end