问题描述
我遇到了这个非常奇怪的现象,我无法弄明白。我有一个管理UITableView的UITableViewController。很简单。我还有一个UISearchDisplayController用于搜索表视图的内容。搜索功能将能够删除表视图显示的内容的项目。因此,如果用户选择删除他们在搜索时找到的项目之一,我不仅要重新加载UISearchDisplayController的表视图,还要重新加载UITableViewController的表视图。当我这样做时,常规表视图的部分弹出并显示在UISearchDisplayController上方。这真的很奇怪。我认为解释它的最好方法是使用图像:
I've run into this really strange phenomenon that I can't quite figure out. I have a UITableViewController that manages a UITableView. Pretty simple. I also have a UISearchDisplayController for searching the contents of the table view. The searching functionality will be able to delete items of the content displayed by the table view. So if the user chooses to delete one of the items they found while searching, I want to not only reload the UISearchDisplayController's table view but also the UITableViewController's table view. When I do that, the sections of the regular table view pop out and display above the UISearchDisplayController. It's really quite strange. I think the best way to explain it is with an image:
如果你们中的任何人知道可能导致此问题或知道什么一个变通方法,这太棒了。
If any of you know what could possibly be causing this problem or know a workaround, that would be fantastic.
推荐答案
再次更新
事实证明,如果桌面的标题在后台重新加载,它会在搜索控制器前弹出,无论如何。
As it turns out, if a table's header is reloaded in the background it pops in front of the search controller no matter what.
我通过禁用fetchedResultsController(设置)解决了这个问题。当搜索消失时,让它在需要时再次加载。
I solved this by disabling the fetchedResultsController (setting it to nil) and letting it load lazily again when needed when the search disappears.
更新 - 下面的原始答案
UPDATED - Original answer below
在我的情况下,我使用两个fetchedResultsControllers,一个用于主tableview,另一个用于搜索。
In my case I'm using two fetchedResultsControllers one for the main tableview and one for the search.
我发现在添加节标题时阻止动画可防止此错误。因此,在searchDisplayController.active中,我只是禁用了部分更改的动画。请参阅下面的代码。
I discovered that preventing animations when adding the section headers prevents this bug. So while searchDisplayController.active I simply disable the animation of the section change. see code below.
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (!self.reordering) {
UITableView *myTableView = controller == __fetchedResultsController ? self.tableView : self.searchDisplayController.searchResultsTableView;
UITableViewRowAnimation animation;
if (self.searchDisplayController.active) {
animation = UITableViewRowAnimationNone;
} else {
animation = UITableViewRowAnimationFade;
}
switch(type)
{
case NSFetchedResultsChangeInsert:
[myTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:animation];
break;
case NSFetchedResultsChangeDelete:
[myTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:animation];
break;
}
}
}
原始答案
另一个答案实际上并不适用于它自己。原因是,显示的标题不是searchDisplayController的tableview中的标题。它是主表视图中的标题,由于某种原因被添加到视图层次结构中的搜索表视图上方。
The other answer doesn't actually work on it's own. The reason is, the header that is showing is not a header in the searchDisplayController's tableview. It's a header from the main tableview that for some reason is being added above the search table view in the view hierarchy.
我通过禁用主表视图的更新解决了这个问题而searchDisplayController.active = YES。
I solved this problem by disabling updates to the main tableview while searchDisplayController.active = YES.
在我的情况下,我正在使用一个延迟加载的获取结果控制器,所以我这样做:
In my case I'm using a lazily loaded fetched results controller so I did it like this:
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
[self.tableView reloadData];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
self.fetchedResultsController.delegate = nil;
self.fetchedResultsController = nil;
}
但是,我仍然有问题,如果我想在主要的reloadData tableview所以在后台看到,节标题仍然漂浮在黑暗区域的前面。
However, I still have the problem that if I want to reloadData on the main tableview so it is seen in the background, the section headers still float in front of the darkened area.
有没有人有更好的解决方案呢?当UISearchDisplayController覆盖数据时重新加载数据似乎是viewForHeaderInSection和titleForHeaderInSection的合法错误。
Does anyone have a better solution for this? It seems like a legitimate bug for viewForHeaderInSection and titleForHeaderInSection when data is reloaded while covered by a UISearchDisplayController.
对我来说,最简单的答案是尝试覆盖搜索视图,这样你就可以了看不到背景表。但这取消了应用程序的Appleness。
The simplest answer for me is to try and override the search view so you can't see the background table. But that takes away from the "Appleness" of the app.
这篇关于在UISearchDisplayController后面重新加载UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!