// // JXSearchViewController.m // AICity // // Created by TogetherWatch on 2025-10-20. // #import "JXSearchViewController.h" #import "JXAPIService.h" #import "JXDrama.h" // Cell复用标识符 static NSString * const kSearchResultCellIdentifier = @"SearchResultCell"; static NSString * const kHotRecommendCellIdentifier = @"HotRecommendCell"; @interface JXSearchViewController () #pragma mark - UI组件 // 顶部搜索栏 @property (nonatomic, strong) UIView *searchBarContainer; @property (nonatomic, strong) UIButton *backButton; @property (nonatomic, strong) UITextField *searchTextField; @property (nonatomic, strong) UIButton *searchButton; // 搜索历史区域 @property (nonatomic, strong) UIView *searchHistoryContainer; @property (nonatomic, strong) UILabel *historyTitleLabel; @property (nonatomic, strong) UIButton *clearHistoryButton; @property (nonatomic, strong) UIView *historyTagsContainer; // 热门推荐区域 @property (nonatomic, strong) UIView *hotDiscoverContainer; @property (nonatomic, strong) UILabel *hotDiscoverTitleLabel; @property (nonatomic, strong) UIView *hotDiscoverListContainer; // 热门推荐列表(未搜索时显示) @property (nonatomic, strong) UIView *hotRecommendContainer; @property (nonatomic, strong) UITableView *hotRecommendTableView; // 搜索结果列表 @property (nonatomic, strong) UIView *searchResultContainer; @property (nonatomic, strong) UITableView *searchResultTableView; @property (nonatomic, strong) UIView *emptyView; #pragma mark - 数据 @property (nonatomic, copy) NSString *currentKeyword; @property (nonatomic, strong) NSMutableArray *searchHistory; @property (nonatomic, strong) NSArray *hotDiscover; @property (nonatomic, strong) NSMutableArray *hotRecommendList; @property (nonatomic, strong) NSMutableArray *searchResultList; #pragma mark - 状态 @property (nonatomic, assign) BOOL isSearching; @property (nonatomic, assign) BOOL isLoadingHotRecommend; @property (nonatomic, assign) BOOL isLoadingSearchResult; @property (nonatomic, assign) NSInteger hotRecommendPage; @property (nonatomic, assign) NSInteger searchResultPage; @end @implementation JXSearchViewController #pragma mark - 初始化 - (instancetype)initWithKeyword:(NSString *)keyword { self = [super init]; if (self) { _currentKeyword = keyword; _searchHistory = [NSMutableArray array]; _hotRecommendList = [NSMutableArray array]; _searchResultList = [NSMutableArray array]; _hotRecommendPage = 1; _searchResultPage = 1; } return self; } - (instancetype)init { return [self initWithKeyword:nil]; } #pragma mark - 生命周期 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self setupUI]; [self loadSearchHistory]; [self loadHotDiscover]; [self loadHotRecommend]; // 如果有初始关键词,执行搜索 if (self.currentKeyword && self.currentKeyword.length > 0) { self.searchTextField.text = self.currentKeyword; [self performSearch:self.currentKeyword]; } } #pragma mark - UI设置 - (void)setupUI { // 搜索栏 [self setupSearchBar]; // 搜索历史 [self setupSearchHistory]; // 热门推荐(热搜榜) [self setupHotDiscover]; // 热门推荐列表 [self setupHotRecommend]; // 搜索结果 [self setupSearchResult]; // 初始状态:显示热门推荐,隐藏搜索结果 self.hotRecommendContainer.hidden = NO; self.searchResultContainer.hidden = YES; } - (void)setupSearchBar { self.searchBarContainer = [[UIView alloc] init]; self.searchBarContainer.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.searchBarContainer]; // 返回按钮 self.backButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.backButton setImage:[UIImage systemImageNamed:@"chevron.left"] forState:UIControlStateNormal]; self.backButton.tintColor = [UIColor blackColor]; [self.backButton addTarget:self action:@selector(handleBack) forControlEvents:UIControlEventTouchUpInside]; [self.searchBarContainer addSubview:self.backButton]; // 搜索输入框 self.searchTextField = [[UITextField alloc] init]; self.searchTextField.placeholder = @"搜索短剧"; self.searchTextField.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0]; self.searchTextField.layer.cornerRadius = 20; self.searchTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 0)]; self.searchTextField.leftViewMode = UITextFieldViewModeAlways; self.searchTextField.returnKeyType = UIReturnKeySearch; self.searchTextField.delegate = self; [self.searchTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged]; [self.searchBarContainer addSubview:self.searchTextField]; // 搜索按钮 self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.searchButton setTitle:@"搜索" forState:UIControlStateNormal]; [self.searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; self.searchButton.backgroundColor = [UIColor systemBlueColor]; self.searchButton.layer.cornerRadius = 20; [self.searchButton addTarget:self action:@selector(handleSearch) forControlEvents:UIControlEventTouchUpInside]; [self.searchBarContainer addSubview:self.searchButton]; // 约束 self.searchBarContainer.translatesAutoresizingMaskIntoConstraints = NO; self.backButton.translatesAutoresizingMaskIntoConstraints = NO; self.searchTextField.translatesAutoresizingMaskIntoConstraints = NO; self.searchButton.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.searchBarContainer.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor], [self.searchBarContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.searchBarContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.searchBarContainer.heightAnchor constraintEqualToConstant:60], [self.backButton.leadingAnchor constraintEqualToAnchor:self.searchBarContainer.leadingAnchor constant:12], [self.backButton.centerYAnchor constraintEqualToAnchor:self.searchBarContainer.centerYAnchor], [self.backButton.widthAnchor constraintEqualToConstant:40], [self.backButton.heightAnchor constraintEqualToConstant:40], [self.searchTextField.leadingAnchor constraintEqualToAnchor:self.backButton.trailingAnchor constant:8], [self.searchTextField.centerYAnchor constraintEqualToAnchor:self.searchBarContainer.centerYAnchor], [self.searchTextField.heightAnchor constraintEqualToConstant:40], [self.searchButton.leadingAnchor constraintEqualToAnchor:self.searchTextField.trailingAnchor constant:8], [self.searchButton.trailingAnchor constraintEqualToAnchor:self.searchBarContainer.trailingAnchor constant:-12], [self.searchButton.centerYAnchor constraintEqualToAnchor:self.searchBarContainer.centerYAnchor], [self.searchButton.widthAnchor constraintEqualToConstant:60], [self.searchButton.heightAnchor constraintEqualToConstant:40] ]]; } - (void)setupSearchHistory { self.searchHistoryContainer = [[UIView alloc] init]; self.searchHistoryContainer.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.searchHistoryContainer]; // 标题 self.historyTitleLabel = [[UILabel alloc] init]; self.historyTitleLabel.text = @"搜索历史"; self.historyTitleLabel.font = [UIFont boldSystemFontOfSize:16]; [self.searchHistoryContainer addSubview:self.historyTitleLabel]; // 清除按钮 self.clearHistoryButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.clearHistoryButton setImage:[UIImage systemImageNamed:@"trash"] forState:UIControlStateNormal]; self.clearHistoryButton.tintColor = [UIColor grayColor]; [self.clearHistoryButton addTarget:self action:@selector(handleClearHistory) forControlEvents:UIControlEventTouchUpInside]; [self.searchHistoryContainer addSubview:self.clearHistoryButton]; // 历史标签容器 self.historyTagsContainer = [[UIView alloc] init]; [self.searchHistoryContainer addSubview:self.historyTagsContainer]; // 约束 self.searchHistoryContainer.translatesAutoresizingMaskIntoConstraints = NO; self.historyTitleLabel.translatesAutoresizingMaskIntoConstraints = NO; self.clearHistoryButton.translatesAutoresizingMaskIntoConstraints = NO; self.historyTagsContainer.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.searchHistoryContainer.topAnchor constraintEqualToAnchor:self.searchBarContainer.bottomAnchor constant:16], [self.searchHistoryContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.searchHistoryContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.searchHistoryContainer.heightAnchor constraintGreaterThanOrEqualToConstant:60], [self.historyTitleLabel.topAnchor constraintEqualToAnchor:self.searchHistoryContainer.topAnchor constant:12], [self.historyTitleLabel.leadingAnchor constraintEqualToAnchor:self.searchHistoryContainer.leadingAnchor constant:16], [self.clearHistoryButton.topAnchor constraintEqualToAnchor:self.searchHistoryContainer.topAnchor constant:12], [self.clearHistoryButton.trailingAnchor constraintEqualToAnchor:self.searchHistoryContainer.trailingAnchor constant:-16], [self.clearHistoryButton.widthAnchor constraintEqualToConstant:30], [self.clearHistoryButton.heightAnchor constraintEqualToConstant:30], [self.historyTagsContainer.topAnchor constraintEqualToAnchor:self.historyTitleLabel.bottomAnchor constant:12], [self.historyTagsContainer.leadingAnchor constraintEqualToAnchor:self.searchHistoryContainer.leadingAnchor constant:16], [self.historyTagsContainer.trailingAnchor constraintEqualToAnchor:self.searchHistoryContainer.trailingAnchor constant:-16], [self.historyTagsContainer.bottomAnchor constraintEqualToAnchor:self.searchHistoryContainer.bottomAnchor constant:-12] ]]; } - (void)setupHotDiscover { self.hotDiscoverContainer = [[UIView alloc] init]; self.hotDiscoverContainer.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.hotDiscoverContainer]; // 标题 self.hotDiscoverTitleLabel = [[UILabel alloc] init]; self.hotDiscoverTitleLabel.text = @"热门搜索"; self.hotDiscoverTitleLabel.font = [UIFont boldSystemFontOfSize:16]; [self.hotDiscoverContainer addSubview:self.hotDiscoverTitleLabel]; // 热搜列表容器 self.hotDiscoverListContainer = [[UIView alloc] init]; [self.hotDiscoverContainer addSubview:self.hotDiscoverListContainer]; // 约束 self.hotDiscoverContainer.translatesAutoresizingMaskIntoConstraints = NO; self.hotDiscoverTitleLabel.translatesAutoresizingMaskIntoConstraints = NO; self.hotDiscoverListContainer.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.hotDiscoverContainer.topAnchor constraintEqualToAnchor:self.searchHistoryContainer.bottomAnchor constant:16], [self.hotDiscoverContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.hotDiscoverContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.hotDiscoverContainer.heightAnchor constraintGreaterThanOrEqualToConstant:200], [self.hotDiscoverTitleLabel.topAnchor constraintEqualToAnchor:self.hotDiscoverContainer.topAnchor constant:12], [self.hotDiscoverTitleLabel.leadingAnchor constraintEqualToAnchor:self.hotDiscoverContainer.leadingAnchor constant:16], [self.hotDiscoverListContainer.topAnchor constraintEqualToAnchor:self.hotDiscoverTitleLabel.bottomAnchor constant:12], [self.hotDiscoverListContainer.leadingAnchor constraintEqualToAnchor:self.hotDiscoverContainer.leadingAnchor constant:16], [self.hotDiscoverListContainer.trailingAnchor constraintEqualToAnchor:self.hotDiscoverContainer.trailingAnchor constant:-16], [self.hotDiscoverListContainer.bottomAnchor constraintEqualToAnchor:self.hotDiscoverContainer.bottomAnchor constant:-12] ]]; } - (void)setupHotRecommend { self.hotRecommendContainer = [[UIView alloc] init]; self.hotRecommendContainer.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.hotRecommendContainer]; self.hotRecommendTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.hotRecommendTableView.delegate = self; self.hotRecommendTableView.dataSource = self; self.hotRecommendTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.hotRecommendTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHotRecommendCellIdentifier]; [self.hotRecommendContainer addSubview:self.hotRecommendTableView]; // 约束 self.hotRecommendContainer.translatesAutoresizingMaskIntoConstraints = NO; self.hotRecommendTableView.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.hotRecommendContainer.topAnchor constraintEqualToAnchor:self.hotDiscoverContainer.bottomAnchor constant:16], [self.hotRecommendContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.hotRecommendContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.hotRecommendContainer.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], [self.hotRecommendTableView.topAnchor constraintEqualToAnchor:self.hotRecommendContainer.topAnchor], [self.hotRecommendTableView.leadingAnchor constraintEqualToAnchor:self.hotRecommendContainer.leadingAnchor], [self.hotRecommendTableView.trailingAnchor constraintEqualToAnchor:self.hotRecommendContainer.trailingAnchor], [self.hotRecommendTableView.bottomAnchor constraintEqualToAnchor:self.hotRecommendContainer.bottomAnchor] ]]; } - (void)setupSearchResult { self.searchResultContainer = [[UIView alloc] init]; self.searchResultContainer.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.searchResultContainer]; self.searchResultTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.searchResultTableView.delegate = self; self.searchResultTableView.dataSource = self; self.searchResultTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.searchResultTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSearchResultCellIdentifier]; [self.searchResultContainer addSubview:self.searchResultTableView]; // 空视图 self.emptyView = [[UIView alloc] init]; self.emptyView.hidden = YES; [self.searchResultContainer addSubview:self.emptyView]; UILabel *emptyLabel = [[UILabel alloc] init]; emptyLabel.text = @"搜索结果为空\n换一个搜索词试试吧~"; emptyLabel.textColor = [UIColor grayColor]; emptyLabel.font = [UIFont systemFontOfSize:14]; emptyLabel.textAlignment = NSTextAlignmentCenter; emptyLabel.numberOfLines = 0; [self.emptyView addSubview:emptyLabel]; // 约束 self.searchResultContainer.translatesAutoresizingMaskIntoConstraints = NO; self.searchResultTableView.translatesAutoresizingMaskIntoConstraints = NO; self.emptyView.translatesAutoresizingMaskIntoConstraints = NO; emptyLabel.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.searchResultContainer.topAnchor constraintEqualToAnchor:self.searchBarContainer.bottomAnchor], [self.searchResultContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.searchResultContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.searchResultContainer.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], [self.searchResultTableView.topAnchor constraintEqualToAnchor:self.searchResultContainer.topAnchor], [self.searchResultTableView.leadingAnchor constraintEqualToAnchor:self.searchResultContainer.leadingAnchor], [self.searchResultTableView.trailingAnchor constraintEqualToAnchor:self.searchResultContainer.trailingAnchor], [self.searchResultTableView.bottomAnchor constraintEqualToAnchor:self.searchResultContainer.bottomAnchor], [self.emptyView.centerXAnchor constraintEqualToAnchor:self.searchResultContainer.centerXAnchor], [self.emptyView.centerYAnchor constraintEqualToAnchor:self.searchResultContainer.centerYAnchor], [self.emptyView.widthAnchor constraintEqualToConstant:200], [self.emptyView.heightAnchor constraintEqualToConstant:100], [emptyLabel.centerXAnchor constraintEqualToAnchor:self.emptyView.centerXAnchor], [emptyLabel.centerYAnchor constraintEqualToAnchor:self.emptyView.centerYAnchor] ]]; } #pragma mark - 数据加载 - (void)loadSearchHistory { // TODO: 从本地存储加载搜索历史 // 暂时使用示例数据 self.searchHistory = [@[@"热门短剧", @"爱情", @"悬疑"] mutableCopy]; [self updateSearchHistoryUI]; } - (void)saveSearchHistory:(NSString *)keyword { if (!keyword || keyword.length == 0) { return; } // 移除重复项 [self.searchHistory removeObject:keyword]; // 插入到最前面 [self.searchHistory insertObject:keyword atIndex:0]; // 最多保存10条 if (self.searchHistory.count > 10) { [self.searchHistory removeLastObject]; } // TODO: 保存到本地存储 [self updateSearchHistoryUI]; } - (void)loadHotDiscover { // TODO: 调用API获取热搜榜 // 暂时使用示例数据 self.hotDiscover = @[@"霸道总裁爱上我", @"穿越时空", @"都市言情", @"甜宠", @"逆袭", @"复仇", @"重生", @"古装"]; [self updateHotDiscoverUI]; } - (void)loadHotRecommend { if (self.isLoadingHotRecommend) { return; } self.isLoadingHotRecommend = YES; [[JXAPIService sharedService] getDramaListWithCategoryId:nil page:self.hotRecommendPage pageSize:20 success:^(id response) { self.isLoadingHotRecommend = NO; NSDictionary *data = response[@"data"]; NSArray *list = data[@"list"]; NSMutableArray *models = [NSMutableArray array]; for (NSDictionary *dict in list) { JXDrama *drama = [[JXDrama alloc] initWithDictionary:dict]; [models addObject:drama]; } [self.hotRecommendList addObjectsFromArray:models]; [self.hotRecommendTableView reloadData]; } failure:^(NSError *error) { self.isLoadingHotRecommend = NO; NSLog(@"[JXSearch] 加载热门推荐失败: %@", error.localizedDescription); }]; } - (void)performSearch:(NSString *)keyword { if (!keyword || keyword.length == 0) { return; } self.isSearching = YES; self.currentKeyword = keyword; self.searchResultPage = 1; [self.searchResultList removeAllObjects]; // 保存搜索历史 [self saveSearchHistory:keyword]; // 切换UI self.hotRecommendContainer.hidden = YES; self.searchResultContainer.hidden = NO; self.searchHistoryContainer.hidden = YES; self.hotDiscoverContainer.hidden = YES; // 执行搜索 [self loadSearchResult]; } - (void)loadSearchResult { if (self.isLoadingSearchResult) { return; } self.isLoadingSearchResult = YES; [[JXAPIService sharedService] searchDramasWithKeyword:self.currentKeyword page:self.searchResultPage pageSize:20 success:^(id response) { self.isLoadingSearchResult = NO; NSDictionary *data = response[@"data"]; NSArray *list = data[@"list"]; NSMutableArray *models = [NSMutableArray array]; for (NSDictionary *dict in list) { JXDrama *drama = [[JXDrama alloc] initWithDictionary:dict]; [models addObject:drama]; } [self.searchResultList addObjectsFromArray:models]; [self.searchResultTableView reloadData]; // 显示/隐藏空视图 self.emptyView.hidden = (self.searchResultList.count > 0); } failure:^(NSError *error) { self.isLoadingSearchResult = NO; NSLog(@"[JXSearch] 搜索失败: %@", error.localizedDescription); }]; } #pragma mark - UI更新 - (void)updateSearchHistoryUI { // 清空现有视图 for (UIView *subview in self.historyTagsContainer.subviews) { [subview removeFromSuperview]; } if (self.searchHistory.count == 0) { self.searchHistoryContainer.hidden = YES; return; } self.searchHistoryContainer.hidden = NO; // 创建历史标签 CGFloat xOffset = 0; CGFloat yOffset = 0; CGFloat maxWidth = self.view.bounds.size.width - 32; for (NSString *keyword in self.searchHistory) { UIButton *tagButton = [self createTagButton:keyword]; CGSize size = [tagButton sizeThatFits:CGSizeMake(maxWidth, 36)]; if (xOffset + size.width > maxWidth) { xOffset = 0; yOffset += 44; } tagButton.frame = CGRectMake(xOffset, yOffset, size.width, 36); [self.historyTagsContainer addSubview:tagButton]; xOffset += size.width + 8; } } - (void)updateHotDiscoverUI { // 清空现有视图 for (UIView *subview in self.hotDiscoverListContainer.subviews) { [subview removeFromSuperview]; } if (!self.hotDiscover || self.hotDiscover.count == 0) { return; } // 创建热搜榜列表 CGFloat yOffset = 0; for (NSInteger i = 0; i < self.hotDiscover.count; i++) { NSString *keyword = self.hotDiscover[i]; UIView *itemView = [self createHotDiscoverItem:keyword index:i]; itemView.frame = CGRectMake(0, yOffset, self.view.bounds.size.width - 32, 44); [self.hotDiscoverListContainer addSubview:itemView]; yOffset += 44; } } - (UIButton *)createTagButton:(NSString *)title { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:14]; button.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0]; button.layer.cornerRadius = 18; button.contentEdgeInsets = UIEdgeInsetsMake(8, 16, 8, 16); [button addTarget:self action:@selector(handleHistoryTagTap:) forControlEvents:UIControlEventTouchUpInside]; return button; } - (UIView *)createHotDiscoverItem:(NSString *)keyword index:(NSInteger)index { UIView *itemView = [[UIView alloc] init]; itemView.userInteractionEnabled = YES; UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 44)]; indexLabel.text = [NSString stringWithFormat:@"%ld", (long)(index + 1)]; indexLabel.font = [UIFont boldSystemFontOfSize:16]; indexLabel.textColor = (index < 3) ? [UIColor systemRedColor] : [UIColor grayColor]; indexLabel.textAlignment = NSTextAlignmentCenter; [itemView addSubview:indexLabel]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, self.view.bounds.size.width - 72, 44)]; titleLabel.text = keyword; titleLabel.font = [UIFont systemFontOfSize:15]; titleLabel.textColor = [UIColor blackColor]; [itemView addSubview:titleLabel]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleHotDiscoverTap:)]; [itemView addGestureRecognizer:tap]; itemView.tag = index; return itemView; } #pragma mark - 事件处理 - (void)handleBack { [self.navigationController popViewControllerAnimated:YES]; } - (void)handleSearch { NSString *keyword = [self.searchTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; [self performSearch:keyword]; [self.searchTextField resignFirstResponder]; } - (void)handleClearHistory { [self.searchHistory removeAllObjects]; [self updateSearchHistoryUI]; // TODO: 清除本地存储 } - (void)handleHistoryTagTap:(UIButton *)sender { NSString *keyword = sender.titleLabel.text; self.searchTextField.text = keyword; [self performSearch:keyword]; } - (void)handleHotDiscoverTap:(UITapGestureRecognizer *)gesture { NSInteger index = gesture.view.tag; if (index < self.hotDiscover.count) { NSString *keyword = self.hotDiscover[index]; self.searchTextField.text = keyword; [self performSearch:keyword]; } } - (void)textFieldDidChange { NSString *text = self.searchTextField.text; if (text.length == 0 && self.isSearching) { // 清空搜索,返回热门推荐 self.isSearching = NO; self.hotRecommendContainer.hidden = NO; self.searchResultContainer.hidden = YES; self.searchHistoryContainer.hidden = NO; self.hotDiscoverContainer.hidden = NO; } } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self handleSearch]; return YES; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.hotRecommendTableView) { return self.hotRecommendList.count; } else { return self.searchResultList.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.hotRecommendTableView) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHotRecommendCellIdentifier forIndexPath:indexPath]; JXDrama *drama = self.hotRecommendList[indexPath.row]; cell.textLabel.text = drama.title; return cell; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSearchResultCellIdentifier forIndexPath:indexPath]; JXDrama *drama = self.searchResultList[indexPath.row]; cell.textLabel.text = drama.title; return cell; } } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; JXDrama *drama; if (tableView == self.hotRecommendTableView) { drama = self.hotRecommendList[indexPath.row]; } else { drama = self.searchResultList[indexPath.row]; } NSLog(@"[JXSearch] 点击短剧: %@", drama.title); // TODO: 跳转到详情页或播放页 } @end