我的 tableView 上有一個 SearchBar。鍵入以搜索表格視圖時,它可以正常作業。但是,當按下取消按鈕時,tableView 不會重新加載資料。
-(void)viewDidLoad {
[super viewDidLoad];
isFiltered = false;
self.searchController = [[UISearchController alloc]
initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
}
- (void)viewWillAppear:(BOOL)animated {
NSBundle *bundle = [NSBundle mainBundle];
self.files = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"thepdfpowerpoints"];
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.title = @"Devo Songs";
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"FILESLOAD%@", self.files);
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
-(void) updateSearchResultsForSearchController:(UISearchController *)searchController {
isFiltered = true;
NSString *searchText = self.searchController.searchBar.text;
searchResults = [[NSMutableArray alloc] init];
for (NSString *file in self.files) {
NSRange nameRange = [file rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[searchResults addObject:file];
}
}
[self.tableView reloadData];
}
-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//self.tableView.tableHeaderView = searchBar;
NSLog(@"Cancel");
isFiltered=false;
[self.tableView reloadData];
}
我在cellForRowAtIndexPath第一次進入視圖時在控制臺中添加了一個 NSLog,但是當我單擊取消按鈕時它沒有被呼叫。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Loading");
行/節數的代碼是:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
if (isFiltered) {
return [searchResults count];
}
else {
return [self.files count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
uj5u.com熱心網友回復:
點擊搜索欄的“取消”按鈕會清除搜索文本...
然后呼叫 updateSearchResultsForSearchController.
因此,您當前的代碼再次設定isFiltered為 true,然后根據空searchResults陣列重新加載表格視圖。
searchBarCancelButtonClicked除非您想做一些除了重繪 表格視圖之外的事情,否則您實際上并不需要實作。
嘗試將您的方法更改為此 -如果沒有在搜索欄位中輸入任何文本updateSearchResultsForSearchController,它將使用完整的陣列。files否則,它將過濾檔案陣列。
-(void) updateSearchResultsForSearchController:(UISearchController *)searchController {
NSLog(@"update"); // just to show when this is being called
NSString *searchText = self.searchController.searchBar.text;
if (searchText.length != 0) {
isFiltered = YES;
searchResults = [[NSMutableArray alloc] init];
for (NSString *file in self.files) {
NSRange nameRange = [file rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[searchResults addObject:file];
}
}
} else {
isFiltered = NO;
}
[self.tableView reloadData];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/434852.html
