在搜索栏中键入第一个字符后应用程序冻结



我在表视图中实现了一个搜索栏。一切都还可以。出于自定义的原因,我把两者都嵌入了视图控制器中。从那时起,在搜索栏中键入第一个字符后,应用程序将冻结。之后暂停/取消暂停应用程序会在调试器中显示不同的点,所以应用程序似乎正在运行,在显示器中我可以看到我在键盘上键入的字符高亮(更大)。玩了一段时间后,我发现,当我第一次选择搜索栏时,但没有键入任何内容并点击取消,这就解决了问题。之后,我可以按预期使用搜索栏。

我在网上搜索了几天,但没有找到任何解决这个问题的线索。因此,任何帮助都是受欢迎的。

这是我的代码:

-(void)viewDidLoad
[super viewDidLoad];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PhaseHintergrund"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
self.searchItem = nil;
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"Suchleiste"]forState:UIControlStateNormal];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
searchBar.placeholder = @"Symptomsuche";
UIImage *myImage = [UIImage imageNamed: @"NavbarTransparent"];
searchBar.backgroundImage = myImage;
searchBar.delegate = self;
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchController.searchResultsTableView.separatorColor = self.tableView.separatorColor;
self.searchController.searchResultsTableView.backgroundView = tempImageView;
self.searchController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.tableView reloadData];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = nil;
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITextView *symptom = (UITextView*) [cell viewWithTag:2];
symptom.text = (NSString*)[object valueForKey:@"name"];
-(BOOL)searchDisplayController:(UISearchDisplayController *)_controller shouldReloadTableForSearchString:(NSString *)searchString 
self.searchItem = searchString;
self.fetchedResultsController = nil;
return YES;
-(void)searchDisplayController:(UISearchDisplayController *)_controller willUnloadSearchResultsTableView:(UITableView *)tableView 
self.searchItem = nil;
self.fetchedResultsController = nil;
[self.tableView reloadData];

编辑在所有UISearchDisplayController委托方法中设置断点时,我发现在出现错误的情况下(之前第一次使用searchbar而没有取消),方法searchDisplayController:didLoadSearchResultsTableView:将不会执行。因此,在我看来,此时没有活动的表视图,这可能会导致问题/错误。

所以问题是,我在初始化过程中遗漏了什么或做错了什么,取消搜索对我来说就是修复?

每次调用-searchDisplayController:shouldReloadTableForSearchString:时,都会生成一个新的提取结果控制器并重新加载表视图。为了表现,你可能经常这样做。

同时,我找到了解决问题的方法,但仍然不明白为什么它现在可以工作。在viewDidLoad中,我在初始化后添加了以下行:

    [self searchDisplayController:self.searchController didLoadSearchResultsTableView:self.searchController.searchResultsTableView];

现在搜索栏也在进行第一次搜索。

相关内容

最新更新