OGeek|极客世界-中国程序员成长平台

标题: ios - 在搜索栏中输入第一个字符后应用程序卡住 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:46
标题: ios - 在搜索栏中输入第一个字符后应用程序卡住

我在表格 View 中实现了一个搜索栏。一切都很好。出于自定义原因,我确实将两者都嵌入到 View Controller 中。由于此时在搜索栏中输入第一个字符后应用程序卡住。之后暂停/取消暂停应用程序会在调试器中显示不同的点,因此应用程序似乎正在运行,并且在显示中我可以看到我在键盘上输入的高亮(更大)的字符。玩了一会儿我发现,当我第一次选择搜索栏,但不输入任何内容并点击取消时,这解决了问题。在此之后,我可以按预期使用搜索栏。

我在互联网上搜索了几天,但没有找到任何解决此问题的提示。所以欢迎任何帮助。

这是我的代码:

-(void)viewDidLoad

[super viewDidLoad];

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed"haseHintergrund"]];
[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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath

UITableViewCell *cell = nil;
cell = [self.tableView dequeueReusableCellWithIdentifier"Cell2" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;

-(void)configureCellUITableViewCell *)cell atIndexPathNSIndexPath *)indexPath

NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITextView *symptom = (UITextView*) [cell viewWithTag:2];
symptom.text = (NSString*)[object valueForKey"name"];

-(BOOL)searchDisplayControllerUISearchDisplayController *)_controller shouldReloadTableForSearchStringNSString *)searchString 

self.searchItem = searchString;
self.fetchedResultsController = nil;
return YES;

-(void)searchDisplayControllerUISearchDisplayController *)_controller willUnloadSearchResultsTableViewUITableView *)tableView 

self.searchItem = nil;
self.fetchedResultsController = nil;
[self.tableView reloadData];

编辑 在所有 UISearchDisplayController 委托(delegate)方法中设置断点,我发现在出错的情况下(第一次使用搜索栏没有取消之前)方法 searchDisplayController: didLoadSearchResultsTableView: 不会被执行。所以在我看来,此时没有事件的 tableview,这可能会导致问题/错误。

所以问题是我在初始化中遗漏了什么或做错了什么,取消搜索对我来说是固定的吗?



Best Answer-推荐答案


每次调用 -searchDisplayController:shouldReloadTableForSearchString: 时,您都会创建一个新的获取结果 Controller 并重新加载您的表格 View 。为了提高性能,您可能过于频繁地这样做了。

关于ios - 在搜索栏中输入第一个字符后应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18408723/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4