取消 SearchBar 时 UITableView 不重新加载数据

UITableView not reloading data when SearchBar is Canceled

我的 tableView 上有一个 SearchBar。在 table 视图中键入搜索时它可以正常工作。但是,当按下取消按钮时,table视图不会重新加载数据。

-(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");

rows/section号码的代码是:

- (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;
    
    
}

点击搜索栏的“取消”按钮可清除搜索文本...

然后调用 updateSearchResultsForSearchController.

因此您当前的代码再次将 isFiltered 设置为 true,然后基于空 searchResults 数组重新加载 table 视图。

你真的不需要实现 searchBarCancelButtonClicked 除非你想做一些除了刷新你的 table 视图之外的事情。

尝试将您的 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];
}