WFCUSearchViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // SearchViewController.h
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/7.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSearchViewController.h"
  9. #import "WFCUConfigManager.h"
  10. @interface WFCUSearchViewController () <UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate>
  11. @property (nonatomic, strong) UITableView *tableView;
  12. @property (nonatomic, strong) UISearchController *searchController;
  13. //数据源
  14. @property (nonatomic, strong) NSMutableArray *dataList;
  15. @property (nonatomic, strong) NSMutableArray *searchList;
  16. @end
  17. @implementation WFCUSearchViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self initSearchUIAndData];
  21. }
  22. - (void)initSearchUIAndData {
  23. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  24. self.navigationItem.title = WFCString(@"AddFriend");
  25. //初始化数据源
  26. _dataList = [NSMutableArray array];
  27. _searchList = [NSMutableArray array];
  28. self.dataList = [NSMutableArray arrayWithCapacity:100];//容量
  29. //产生100个“数字+三个随机字母”
  30. for (NSInteger i = 0; i < 100; i++)
  31. {
  32. [self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];
  33. }
  34. CGFloat screenWidth = self.view.frame.size.width;
  35. CGFloat screenHeight = self.view.frame.size.height;
  36. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
  37. //设置代理
  38. _tableView.delegate = self;
  39. _tableView.dataSource = self;
  40. _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//取消分割线
  41. //创建UISearchController
  42. _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
  43. //设置代理
  44. _searchController.delegate = self;
  45. _searchController.searchResultsUpdater = self;
  46. //设置UISearchController的显示属性,以下3个属性默认为YES
  47. //搜索时,背景变暗色
  48. // _searchController.dimsBackgroundDuringPresentation = NO;
  49. //搜索时,背景变模糊
  50. // _searchController.obscuresBackgroundDuringPresentation = NO;
  51. //隐藏导航栏
  52. // _searchController.hidesNavigationBarDuringPresentation = NO;
  53. _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
  54. // 添加 searchbar 到 headerview
  55. UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
  56. _searchController.searchBar.frame = container.bounds;
  57. [container addSubview:_searchController.searchBar];
  58. self.tableView.tableHeaderView = container;
  59. [self.view addSubview:_tableView];
  60. }
  61. //生成三个随机字母
  62. - (NSString *)shuffledAlphabet
  63. {
  64. NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",
  65. @"H",@"I",@"J",@"K",@"L",@"M",@"N",
  66. @"O",@"P",@"Q",@"R",@"S",@"T",@"U",
  67. @"V",@"W",@"X",@"Y",@"Z"]];
  68. NSString *strTest = [[NSString alloc]init];
  69. for (int i = 0; i < 3; i++)
  70. {
  71. int x = arc4random() % 25;//0~24 的随机数
  72. strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
  73. }
  74. return strTest;
  75. }
  76. #pragma mark - UITableViewDataSource
  77. //table 返回的行数
  78. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  79. if (self.searchController.active) {
  80. return [self.searchList count];
  81. } else {
  82. return [self.dataList count];
  83. }
  84. }
  85. //返回单元格内容
  86. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  87. static NSString *flag = @"cell";
  88. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag];
  89. if (cell == nil) {
  90. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
  91. }
  92. if (self.searchController.active)//如果正在搜索
  93. {
  94. [cell.textLabel setText:self.searchList[indexPath.row]];
  95. }
  96. else//如果没有搜索
  97. {
  98. [cell.textLabel setText:self.dataList[indexPath.row]];
  99. }
  100. return cell;
  101. }
  102. #pragma mark - UISearchControllerDelegate
  103. //测试UISearchController的执行过程
  104. - (void)willPresentSearchController:(UISearchController *)searchController
  105. {
  106. NSLog(@"willPresentSearchController");
  107. }
  108. - (void)didPresentSearchController:(UISearchController *)searchController
  109. {
  110. NSLog(@"didPresentSearchController");
  111. }
  112. - (void)willDismissSearchController:(UISearchController *)searchController
  113. {
  114. NSLog(@"willDismissSearchController");
  115. }
  116. - (void)didDismissSearchController:(UISearchController *)searchController
  117. {
  118. NSLog(@"didDismissSearchController");
  119. _searchController.searchBar.frame = self.tableView.tableHeaderView.bounds;
  120. }
  121. - (void)presentSearchController:(UISearchController *)searchController
  122. {
  123. NSLog(@"presentSearchController");
  124. }
  125. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController
  126. {
  127. NSLog(@"updateSearchResultsForSearchController");
  128. NSString *searchString = [self.searchController.searchBar text];
  129. NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
  130. if (self.searchList!= nil)
  131. {
  132. [self.searchList removeAllObjects];
  133. }
  134. //过滤数据
  135. self.searchList = [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
  136. //刷新表格
  137. [self.tableView reloadData];
  138. }
  139. - (void)dealloc {
  140. _tableView = nil;
  141. _searchController = nil;
  142. _dataList = nil;
  143. _searchList = nil;
  144. }
  145. @end