WFCUSearchChannelViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // WFCUAddFriendViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/7.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSearchChannelViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUChannelProfileViewController.h"
  11. #import "SDWebImage.h"
  12. #import "MBProgressHUD.h"
  13. @interface WFCUSearchChannelViewController () <UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate>
  14. @property (nonatomic, strong) UITableView *tableView;
  15. @property (nonatomic, strong) UISearchController *searchController;
  16. @property (nonatomic, strong) NSArray *searchList;
  17. @property (nonatomic, strong) NSTimer *timer;
  18. @end
  19. @implementation WFCUSearchChannelViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self initSearchUIAndData];
  23. }
  24. - (void)viewWillDisappear:(BOOL)animated {
  25. [super viewWillDisappear:animated];
  26. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  27. }
  28. - (void)initSearchUIAndData {
  29. self.view.backgroundColor = [UIColor whiteColor];
  30. self.navigationItem.title = @"收听频道";
  31. _searchList = [NSMutableArray array];
  32. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  33. self.searchController.searchResultsUpdater = self;
  34. self.searchController.dimsBackgroundDuringPresentation = NO;
  35. if (@available(iOS 9.1, *)) {
  36. self.searchController.obscuresBackgroundDuringPresentation = NO;
  37. }
  38. [self.searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
  39. self.searchController.searchBar.placeholder = @"搜索频道";
  40. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  41. [self.view addSubview:self.tableView];
  42. self.tableView.delegate = self;
  43. self.tableView.dataSource = self;
  44. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  45. if (@available(iOS 11.0, *)) {
  46. self.navigationItem.searchController = _searchController;
  47. self.navigationItem.hidesSearchBarWhenScrolling = false;
  48. _searchController.hidesNavigationBarDuringPresentation = YES;
  49. } else {
  50. self.tableView.tableHeaderView = _searchController.searchBar;
  51. }
  52. self.definesPresentationContext = YES;
  53. [self.view addSubview:_tableView];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated {
  56. [super viewWillAppear:animated];
  57. self.tabBarController.tabBar.hidden = YES;
  58. }
  59. #pragma mark - UITableViewDataSource
  60. //table 返回的行数
  61. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  62. if (self.searchController.active) {
  63. return [self.searchList count];
  64. } else {
  65. return 0;
  66. }
  67. }
  68. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  69. if (self.searchController.active) {
  70. WFCCChannelInfo *channelInfo = self.searchList[indexPath.row];
  71. WFCUChannelProfileViewController *pvc = [[WFCUChannelProfileViewController alloc] init];
  72. pvc.channelInfo = channelInfo;
  73. [self.navigationController pushViewController:pvc animated:YES];
  74. }
  75. }
  76. //返回单元格内容
  77. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  78. static NSString *flag = @"cell";
  79. if (self.searchController.active) {
  80. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag];
  81. if (cell == nil) {
  82. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
  83. }
  84. WFCCChannelInfo *channelInfo = self.searchList[indexPath.row];
  85. [cell.textLabel setText:channelInfo.name];
  86. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:channelInfo.portrait] placeholderImage:[UIImage imageNamed:@"PersonalChat"]];
  87. cell.userInteractionEnabled = YES;
  88. return cell;
  89. }
  90. else//如果没有搜索
  91. {
  92. return nil;
  93. }
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. return 56;
  97. }
  98. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  99. if (self.searchController.active) {
  100. [self.searchController.searchBar resignFirstResponder];
  101. }
  102. }
  103. #pragma mark - UISearchControllerDelegate
  104. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  105. if (self.timer.valid) {
  106. [self.timer invalidate];
  107. self.timer = nil;
  108. }
  109. self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(onSearch:) userInfo:nil repeats:NO];
  110. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  111. }
  112. - (void)onSearch:(id)sender {
  113. __weak typeof(self) ws = self;
  114. NSString *searchString = [ws.searchController.searchBar text];
  115. if (searchString.length) {
  116. [[WFCCIMService sharedWFCIMService] searchChannel:searchString
  117. success:^(NSArray<WFCCChannelInfo *> *machedChannels) {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. ws.searchList = machedChannels;
  120. [ws.tableView reloadData];
  121. });
  122. }
  123. error:^(int errorCode) {
  124. dispatch_async(dispatch_get_main_queue(), ^{
  125. ws.searchList = nil;
  126. [ws.tableView reloadData];
  127. });
  128. NSLog(@"Search failed, errorCode(%d)", errorCode);
  129. }];
  130. } else {
  131. ws.searchList = nil;
  132. [ws.tableView reloadData];
  133. }
  134. }
  135. - (void)dealloc {
  136. _tableView = nil;
  137. _searchController = nil;
  138. _searchList = nil;
  139. }
  140. @end