iPhone - Проблемы с UitableView - Данные перезагрузить и нет хитов
-
19-09-2019 - |
Вопрос
Я использую этот пример примера кода Apple:
http://developer.apple.com/iphone/library/samplecode/tablesearch/index.html
В этом примере UitableView Получите список контента при запуске. Нажав в Uisearchbar и Typing, список контента будет отфильтрован, также проверяя объем Scopebar.
Я должен восстановить этот вид «мгновенного поиска» в «обычный поиск»: в начале у меня нет данных для обзора Table. Пользователь должен нажать на SearchBar, ввести что -то, нажмите кнопку «Поиск», и запрос поиска будет отправлен на веб -сервер. Ответ WebServer будет помещен в TableView, и пользователь может переключить область, чтобы отфильтровать набор результатов. Изменение значения SearchBar не фильтрует список результатов. Только нажатие «Поиск» инициирует запрос на поиск.
Я взял пример кода и перестроил его (исходный код внизу). Но у меня есть две проблемы с этим.
- При первоначальном вызове SearchViewController (с Tabbar, SearchBar, Scopebar, TableView) все в порядке. Там есть пустая таблица. Но, щелкнув в поисковой панели и печатает один символ Ony, есть сообщение о том, что «нет хитов». Как я мог избежать этого? Это сообщение должно появиться только в том случае, если пользователь нажимает «Поиск», и на самом деле нет совпадений.
- Моя вторая проблема: набрать «Привет» и нажатие «Поиск». В таблице не указаны результаты. Если я нажимаю на «прерван» или по другой областям, результаты будут перечислены. Так должно быть что -то вроде отсутствующего «перезагрузки»?!
Я надеюсь, что кто -то сможет мне помочь. Большое спасибо в Adavence и с наилучшими пожеланиями.
Мой исходный код:
@implementation SearchViewController
@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
- (void)viewDidLoad {
// restore search settings if they were saved in didReceiveMemoryWarning.
if (self.savedSearchTerm) {
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
}
- (void)viewDidUnload {
// Save the state of the search UI so that it can be restored if the view is re-created.
self.searchWasActive = [self.searchDisplayController isActive];
self.savedSearchTerm = [self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
self.filteredListContent = nil;
}
- (void)dealloc {
[listContent release];
[filteredListContent release];
[super dealloc];
}
- (void)setData {
self.listContent = [NSMutableArray arrayWithCapacity:3];
[self.listContent addObject:[SearchObjects itemWithType:@"AAA" name:@"Test1"]];
[self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test2"]];
[self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test3"]];
// create a filtered list
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.filteredListContent count];
} else {
return [self.listContent count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/* If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list. */
SearchObjects *searchObject = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
searchObject = [self.filteredListContent objectAtIndex:indexPath.row];
} else {
searchObject = [self.listContent objectAtIndex:indexPath.row];
}
cell.textLabel.text = searchObject.name;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// HERE IS THE SOURCE CODE FOR PUSHING TO THE NEXT VIEW
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// DO SOME CALCULATIONS… AND THE setData METHOD IS CALLED
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
/* Update the filtered array based on the search text and scope. */
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/* Search the main list for whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. */
for (SearchObjects *searchObject in listContent) {
if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) {
NSComparisonResult result = [searchObject.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame) {
[self.filteredListContent addObject:searchObject];
}
}
}
}
- (void)filterContentForScope:(NSString*)scope {
/* Update the filtered array based on the search text and scope. */
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/* Search the main list for whose type matches the scope (if selected); add items that match to the filtered array. */
for (SearchObjects *searchObject in listContent) {
if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) {
[self.filteredListContent addObject:searchObject];
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
@end
Решение 2
Проблема решается, см. В комментариях.
Другие советы
Для вашей первой проблемы вы должны настроить делегат для строки поиска, а затем реализовать – searchBarSearchButtonClicked:
и поместите там свой поисковый код. Вам также может придется реализовать других, таких как – searchBarTextDidEndEditing:
или же – searchBar:textDidChange:
И убедитесь, что они не выполняют поиск.
Для вашего второго вопроса вы можете просто перезагрузить TableView, используя делегат снова из – searchBarSearchButtonClicked:
Чтобы это произошло после того, как вы уже искали. Вы можете использовать [tableView reloadData]
Чтобы сделать это.