当 SearchBar 取消时,UITableView 不重新加载数据
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
点击搜索栏的“取消”按钮会清除搜索文本...
然后调用
updateSearchResultsForSearchController
。因此,您当前的代码再次将
isFiltered
设置为 true,然后根据空的searchResults
数组重新加载表视图。您实际上并不需要实现
searchBarCancelButtonClicked
,除非您想要执行除刷新表视图之外的其他操作。尝试将您的
updateSearchResultsForSearchController
方法更改为此 - 如果在搜索字段中未输入任何文本,它将使用完整的files
数组。否则,它将过滤文件数组。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 emptysearchResults
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 fullfiles
array if no text has been entered in the search field. Otherwise, it will filter the files array.