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

发布于 2025-01-10 02:22:09 字数 2945 浏览 2 评论 0原文

我的 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;
    
    
}

I have a SearchBar on my tableView. It is correctly working when typing to search through the table view. However, when the cancel button is pressed, the tableView is not reloading the data.

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

I have added an NSLog in cellForRowAtIndexPath This is showing in console when I first go to the view, but it is not being called when I click the cancel button.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"Loading");

The code for number of rows/section is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻安 2025-01-17 02:22:09

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

然后调用 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];
}

Tapping the search bar's "Cancel" button clears the search text...

and then calls updateSearchResultsForSearchController.

So your current code is setting isFiltered to true again, and then reloading the table view based on an empty searchResults array.

You don't really need to implement searchBarCancelButtonClicked unless you want to do something other than refresh your table view.

Try changing your updateSearchResultsForSearchController method to this - it will use the full files array if no text has been entered in the search field. Otherwise, it will filter the files array.

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文