// // SASearchVC.m // SamaVoice // // Created by 申中迪 on 2022/5/10. // #import "SASearchVC.h" #import "SASearchNavView.h" #import "SASearchHistoryView.h" #import "SASearchCell.h" @interface SASearchVC () @property (nonatomic, strong) SASearchNavView *navView; @property (nonatomic, strong) SASearchHistoryView *historyView; @property (nonatomic, strong) NSArray *userArray; @property (nonatomic, strong) NSArray *roomArray; @end @implementation SASearchVC - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.navView.searchTF becomeFirstResponder]; } - (void)viewDidLoad { [super viewDidLoad]; [self createUI]; } -(void)onSearchRequest:(NSString *)keyword { [self.view endEditing:YES]; [self.historyView onInsertKeyword:keyword]; NSDictionary *params = @{@"keywords":keyword}; [AFNetworkRequset.shared postRequestWithParams:params Path:@"api/player/search" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) { self.historyView.hidden = YES; self.tableView.hidden = NO; self.userArray = [SASearchModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"][@"user"]]; self.roomArray = [SASearchModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"][@"room"]]; [self.tableView reloadData]; if (self.userArray.count <= 0 && self.roomArray.count <= 0) { [self showNoContentView]; } else { [self hideNoContentView]; } } Failure:^(id _Nonnull errorData) { }]; } -(void)createUI { [self.view addSubview:self.navView]; [self.view addSubview:self.historyView]; self.tableView.hidden = YES; [self.tableView registerNib:[UINib nibWithNibName:@"SASearchCell" bundle:nil] forCellReuseIdentifier:@"SASearchCell"]; self.tableView.rowHeight = 90; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (1 == section) { return self.userArray.count; }else { return self.roomArray.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SASearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SASearchCell" forIndexPath:indexPath]; cell.selectionStyle = NO; if (1 == indexPath.section) { SASearchModel *model = self.userArray[indexPath.row]; [cell onUpdateSASearchCell:model isRoom:NO]; }else { SASearchModel *model = self.roomArray[indexPath.row]; [cell onUpdateSASearchCell:model isRoom:YES]; } return cell; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (1 == section) { if (self.userArray.count > 0) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 36)]; view.backgroundColor = [UIColor clearColor]; [ControlCreator createLabel:view rect:CGRectMake(16, 12, 60, 15) text:@"相关用户" font:YBMediumFont(14) color:HEXCOLOR(0x333333) backguoundColor:nil align:NSTextAlignmentLeft lines:1]; // UIView *line = [ControlCreator createView:view rect:CGRectMake(24, 32, 36, 4) backguoundColor:mainDeepColor]; // line.layer.cornerRadius = 2; return view; }else { return nil; } }else { if (self.roomArray.count > 0) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 36)]; view.backgroundColor = [UIColor clearColor]; [ControlCreator createLabel:view rect:CGRectMake(16, 12, 60, 15) text:@"相关房间" font:YBMediumFont(14) color:HEXCOLOR(0x333333) backguoundColor:nil align:NSTextAlignmentLeft lines:1]; // UIView *line = [ControlCreator createView:view rect:CGRectMake(24, 32, 36, 4) backguoundColor:mainDeepColor]; // line.layer.cornerRadius = 2; return view; }else { return nil; } } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (1 == section) { return self.userArray.count > 0 ? 36 : 0; }else { return self.roomArray.count > 0 ? 36 : 0; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (1 == indexPath.section) { SASearchModel *model = self.userArray[indexPath.row]; [UIViewController goUserMainpageWith:model.uid withRid:@""]; }else { SASearchModel *model = self.roomArray[indexPath.row]; [UIViewController goMicRoomWithRid:model.rid withPwd:@""]; } } - (SASearchNavView *)navView { if (!_navView) { _navView = [[NSBundle mainBundle] loadNibNamed:@"SASearchNavView" owner:self options:nil].firstObject; _navView.frame = CGRectMake(0, yb_StatusBar_H, ScreenWidth, 44); WEAK_SELF _navView.onSearchBlock = ^(NSString * _Nonnull str) { [weakSelf onSearchRequest:str]; }; [_navView.searchTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } return _navView; } - (SASearchHistoryView *)historyView { if (!_historyView) { _historyView = [[SASearchHistoryView alloc] initWithFrame:CGRectMake(0, TOP_BAR_HEIGHT, ScreenWidth, 500)]; WEAK_SELF _historyView.onClickHistoryBlock = ^(NSString * _Nonnull str) { weakSelf.navView.searchTF.text = str; [weakSelf onSearchRequest:str]; }; } return _historyView; } - (void)textFieldDidChange:(UITextField *)tf { if (tf.text.length <= 0) { self.historyView.hidden = NO; self.tableView.hidden = YES; } } @end