处理搜索bug
This commit is contained in:
@@ -74,7 +74,9 @@
|
|||||||
[self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
make.centerY.equalTo(self.contentView);
|
make.centerY.equalTo(self.contentView);
|
||||||
make.right.equalTo(self.contentView).offset(-18);
|
make.right.equalTo(self.contentView).offset(-18);
|
||||||
make.width.height.mas_equalTo(16);
|
make.width.mas_equalTo(6);
|
||||||
|
make.height.mas_equalTo(8);
|
||||||
|
|
||||||
}];
|
}];
|
||||||
|
|
||||||
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
@@ -165,9 +167,13 @@
|
|||||||
@property (nonatomic, strong) UIImageView *searchIconView;
|
@property (nonatomic, strong) UIImageView *searchIconView;
|
||||||
@property (nonatomic, strong) UITextField *searchField;
|
@property (nonatomic, strong) UITextField *searchField;
|
||||||
@property (nonatomic, strong) BaseTableView *tableView;
|
@property (nonatomic, strong) BaseTableView *tableView;
|
||||||
|
@property (nonatomic, strong) UIView *searchResultContainer;
|
||||||
|
@property (nonatomic, strong) BaseTableView *searchResultTableView;
|
||||||
|
|
||||||
@property (nonatomic, strong) NSArray<KBPersonaModel *> *personas;
|
@property (nonatomic, strong) NSArray<KBPersonaModel *> *personas;
|
||||||
@property (nonatomic, strong) NSArray<KBPersonaModel *> *displayPersonas;
|
@property (nonatomic, strong) NSArray<KBPersonaModel *> *displayPersonas;
|
||||||
|
@property (nonatomic, strong) NSArray<KBPersonaModel *> *searchResults;
|
||||||
|
@property (nonatomic, assign) BOOL isShowingSearchResults;
|
||||||
@property (nonatomic, assign) NSInteger currentPage;
|
@property (nonatomic, assign) NSInteger currentPage;
|
||||||
@property (nonatomic, assign) BOOL hasMore;
|
@property (nonatomic, assign) BOOL hasMore;
|
||||||
|
|
||||||
@@ -193,6 +199,8 @@
|
|||||||
[self.searchContainer addSubview:self.searchIconView];
|
[self.searchContainer addSubview:self.searchIconView];
|
||||||
[self.searchContainer addSubview:self.searchField];
|
[self.searchContainer addSubview:self.searchField];
|
||||||
[self.contentView addSubview:self.tableView];
|
[self.contentView addSubview:self.tableView];
|
||||||
|
[self.contentView addSubview:self.searchResultContainer];
|
||||||
|
[self.searchResultContainer addSubview:self.searchResultTableView];
|
||||||
|
|
||||||
[self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
make.edges.equalTo(self);
|
make.edges.equalTo(self);
|
||||||
@@ -227,6 +235,15 @@
|
|||||||
make.left.right.bottom.equalTo(self.contentView);
|
make.left.right.bottom.equalTo(self.contentView);
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
[self.searchResultContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
make.top.equalTo(self.searchContainer.mas_bottom).offset(10);
|
||||||
|
make.left.right.bottom.equalTo(self.contentView);
|
||||||
|
}];
|
||||||
|
|
||||||
|
[self.searchResultTableView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
make.edges.equalTo(self.searchResultContainer);
|
||||||
|
}];
|
||||||
|
|
||||||
__weak typeof(self) weakSelf = self;
|
__weak typeof(self) weakSelf = self;
|
||||||
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
|
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
|
||||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||||
@@ -254,6 +271,7 @@
|
|||||||
if (self.personas.count == 0) {
|
if (self.personas.count == 0) {
|
||||||
self.currentPage = 1;
|
self.currentPage = 1;
|
||||||
self.hasMore = YES;
|
self.hasMore = YES;
|
||||||
|
NSLog(@"[SidebarSearch] 请求人设数据: page=%ld", (long)self.currentPage);
|
||||||
if ([self.delegate respondsToSelector:@selector(personaSidebarView:requestPersonasAtPage:)]) {
|
if ([self.delegate respondsToSelector:@selector(personaSidebarView:requestPersonasAtPage:)]) {
|
||||||
[self.delegate personaSidebarView:self requestPersonasAtPage:self.currentPage];
|
[self.delegate personaSidebarView:self requestPersonasAtPage:self.currentPage];
|
||||||
}
|
}
|
||||||
@@ -271,7 +289,12 @@
|
|||||||
// HomeVC 传入的是全量列表,这里直接替换,避免重复
|
// HomeVC 传入的是全量列表,这里直接替换,避免重复
|
||||||
self.personas = personas ?: @[];
|
self.personas = personas ?: @[];
|
||||||
self.currentPage = safePage;
|
self.currentPage = safePage;
|
||||||
|
NSLog(@"[SidebarSearch] 更新人设: count=%ld, page=%ld, hasMore=%@",
|
||||||
|
(long)self.personas.count, (long)self.currentPage, self.hasMore ? @"YES" : @"NO");
|
||||||
[self applyFilterAndReload];
|
[self applyFilterAndReload];
|
||||||
|
if (self.isShowingSearchResults && self.searchField.text.length > 0) {
|
||||||
|
[self performSearch];
|
||||||
|
}
|
||||||
[self endLoadingMore];
|
[self endLoadingMore];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,31 +320,67 @@
|
|||||||
#pragma mark - Search
|
#pragma mark - Search
|
||||||
|
|
||||||
- (void)searchFieldChanged:(UITextField *)textField {
|
- (void)searchFieldChanged:(UITextField *)textField {
|
||||||
[self applyFilterAndReload];
|
NSString *keyword = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||||
|
NSLog(@"[SidebarSearch] 输入变化: \"%@\"", keyword);
|
||||||
|
if (keyword.length == 0) {
|
||||||
|
[self hideSearchResults];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)applyFilterAndReload {
|
- (void)applyFilterAndReload {
|
||||||
NSString *keyword = [self.searchField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
self.displayPersonas = self.personas;
|
||||||
if (keyword.length == 0) {
|
|
||||||
self.displayPersonas = self.personas;
|
|
||||||
} else {
|
|
||||||
NSMutableArray *result = [NSMutableArray array];
|
|
||||||
for (KBPersonaModel *persona in self.personas) {
|
|
||||||
NSString *name = persona.name ?: @"";
|
|
||||||
NSString *desc = persona.shortDesc ?: persona.introText ?: @"";
|
|
||||||
if ([name localizedCaseInsensitiveContainsString:keyword] ||
|
|
||||||
[desc localizedCaseInsensitiveContainsString:keyword]) {
|
|
||||||
[result addObject:persona];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.displayPersonas = result;
|
|
||||||
}
|
|
||||||
[self.tableView reloadData];
|
[self.tableView reloadData];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)performSearch {
|
||||||
|
NSString *keyword = [self.searchField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||||
|
NSLog(@"[SidebarSearch] 执行搜索: \"%@\" (total=%ld)", keyword, (long)self.personas.count);
|
||||||
|
if (keyword.length == 0) {
|
||||||
|
[self hideSearchResults];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableArray *result = [NSMutableArray array];
|
||||||
|
for (KBPersonaModel *persona in self.personas) {
|
||||||
|
NSString *name = persona.name ?: @"";
|
||||||
|
NSString *desc = persona.shortDesc ?: persona.introText ?: @"";
|
||||||
|
if ([name localizedCaseInsensitiveContainsString:keyword] ||
|
||||||
|
[desc localizedCaseInsensitiveContainsString:keyword]) {
|
||||||
|
[result addObject:persona];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.searchResults = result;
|
||||||
|
NSLog(@"[SidebarSearch] 搜索结果: %ld 条", (long)self.searchResults.count);
|
||||||
|
if (self.searchResults.count > 0) {
|
||||||
|
[self showSearchResults];
|
||||||
|
[self.searchResultTableView reloadData];
|
||||||
|
} else {
|
||||||
|
[self hideSearchResults];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)showSearchResults {
|
||||||
|
self.isShowingSearchResults = YES;
|
||||||
|
self.tableView.hidden = YES;
|
||||||
|
self.searchResultContainer.hidden = NO;
|
||||||
|
[self.contentView bringSubviewToFront:self.searchResultContainer];
|
||||||
|
NSLog(@"[SidebarSearch] 显示搜索结果视图");
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)hideSearchResults {
|
||||||
|
self.isShowingSearchResults = NO;
|
||||||
|
self.searchResults = @[];
|
||||||
|
self.searchResultContainer.hidden = YES;
|
||||||
|
self.tableView.hidden = NO;
|
||||||
|
NSLog(@"[SidebarSearch] 隐藏搜索结果视图");
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - UITableViewDataSource
|
#pragma mark - UITableViewDataSource
|
||||||
|
|
||||||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
||||||
|
if (tableView == self.searchResultTableView) {
|
||||||
|
return self.searchResults.count;
|
||||||
|
}
|
||||||
return self.displayPersonas.count;
|
return self.displayPersonas.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +388,9 @@
|
|||||||
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||||
KBAIPersonaSidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KBAIPersonaSidebarCell"
|
KBAIPersonaSidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KBAIPersonaSidebarCell"
|
||||||
forIndexPath:indexPath];
|
forIndexPath:indexPath];
|
||||||
KBPersonaModel *persona = self.displayPersonas[indexPath.row];
|
KBPersonaModel *persona = (tableView == self.searchResultTableView)
|
||||||
|
? self.searchResults[indexPath.row]
|
||||||
|
: self.displayPersonas[indexPath.row];
|
||||||
BOOL selected = (persona.personaId == self.selectedPersonaId);
|
BOOL selected = (persona.personaId == self.selectedPersonaId);
|
||||||
[cell configureWithPersona:persona selected:selected];
|
[cell configureWithPersona:persona selected:selected];
|
||||||
return cell;
|
return cell;
|
||||||
@@ -342,12 +403,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||||
if (indexPath.row >= self.displayPersonas.count) {
|
NSArray *source = (tableView == self.searchResultTableView) ? self.searchResults : self.displayPersonas;
|
||||||
|
if (indexPath.row >= source.count) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
KBPersonaModel *persona = self.displayPersonas[indexPath.row];
|
KBPersonaModel *persona = source[indexPath.row];
|
||||||
self.selectedPersonaId = persona.personaId;
|
self.selectedPersonaId = persona.personaId;
|
||||||
[self.tableView reloadData];
|
[self.tableView reloadData];
|
||||||
|
if (self.isShowingSearchResults) {
|
||||||
|
[self.searchResultTableView reloadData];
|
||||||
|
}
|
||||||
if ([self.delegate respondsToSelector:@selector(personaSidebarView:didSelectPersona:)]) {
|
if ([self.delegate respondsToSelector:@selector(personaSidebarView:didSelectPersona:)]) {
|
||||||
[self.delegate personaSidebarView:self didSelectPersona:persona];
|
[self.delegate personaSidebarView:self didSelectPersona:persona];
|
||||||
}
|
}
|
||||||
@@ -397,7 +462,9 @@
|
|||||||
_searchField.font = [UIFont systemFontOfSize:14];
|
_searchField.font = [UIFont systemFontOfSize:14];
|
||||||
_searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
_searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
||||||
_searchField.returnKeyType = UIReturnKeySearch;
|
_searchField.returnKeyType = UIReturnKeySearch;
|
||||||
|
_searchField.delegate = self;
|
||||||
[_searchField addTarget:self action:@selector(searchFieldChanged:) forControlEvents:UIControlEventEditingChanged];
|
[_searchField addTarget:self action:@selector(searchFieldChanged:) forControlEvents:UIControlEventEditingChanged];
|
||||||
|
[_searchField addTarget:self action:@selector(searchFieldReturnTapped:) forControlEvents:UIControlEventEditingDidEndOnExit];
|
||||||
}
|
}
|
||||||
return _searchField;
|
return _searchField;
|
||||||
}
|
}
|
||||||
@@ -415,4 +482,46 @@
|
|||||||
return _tableView;
|
return _tableView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (UIView *)searchResultContainer {
|
||||||
|
if (!_searchResultContainer) {
|
||||||
|
_searchResultContainer = [[UIView alloc] init];
|
||||||
|
_searchResultContainer.backgroundColor = [UIColor clearColor];
|
||||||
|
_searchResultContainer.hidden = YES;
|
||||||
|
}
|
||||||
|
return _searchResultContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BaseTableView *)searchResultTableView {
|
||||||
|
if (!_searchResultTableView) {
|
||||||
|
_searchResultTableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
||||||
|
_searchResultTableView.backgroundColor = [UIColor clearColor];
|
||||||
|
_searchResultTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
||||||
|
_searchResultTableView.showsVerticalScrollIndicator = NO;
|
||||||
|
_searchResultTableView.delegate = self;
|
||||||
|
_searchResultTableView.dataSource = self;
|
||||||
|
[_searchResultTableView registerClass:[KBAIPersonaSidebarCell class] forCellReuseIdentifier:@"KBAIPersonaSidebarCell"];
|
||||||
|
}
|
||||||
|
return _searchResultTableView;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - UITextFieldDelegate
|
||||||
|
|
||||||
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
||||||
|
if (textField == self.searchField) {
|
||||||
|
NSLog(@"[SidebarSearch] textFieldShouldReturn");
|
||||||
|
[textField resignFirstResponder];
|
||||||
|
[self performSearch];
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)searchFieldReturnTapped:(UITextField *)textField {
|
||||||
|
if (textField == self.searchField) {
|
||||||
|
NSLog(@"[SidebarSearch] EditingDidEndOnExit");
|
||||||
|
[textField resignFirstResponder];
|
||||||
|
[self performSearch];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user