ios – 当使用beginUpdates / endUpdates时,UISearchBar从tableHeaderView消失

我有一个表视图控制器与一个UISearchController设置UISearchBar作为tableView.tableHeaderView.更新搜索结果时,我使用beginUpdates和endUpdates和相关方法更新表视图的数据.

这使搜索栏消失; tableHeaderView设置为与搜索栏大小相同的空的通用UIView.如果我只是使用reloadData而不是整个beginUpdates / endUpdates过程,一切都很好.

表视图控制器嵌入在常规视图控制器中;没有导航控制器.这是整个执行表视图控制器所必需的重现问题:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;

    self.tableView.tableHeaderView = self.searchController.searchBar;
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

为什么这会导致搜索栏被空白的视图所替代,如何才能避免?

解决方法

当您首次点击UISearchController的searchBar时,searchBar的动画需要大约0.5秒.但是由委托方法立即调用self.tableView.endUpdates(),动画被中断.
searchBar卡在动画的中间,你看不到它的UI:

一个解决方法是检查searchBar是否在动画中,并在其进入​​时延迟self.tableView.endUpdates().

func updateSearchResultsForSearchController(searchController: UISearchController){
    self.tableView.beginUpdates()
    //Do some updates

    if !searchController.searchBar.showsCancelButton{
        self.tableView.performSelector(#selector(UITableView.endUpdates),withObject: nil,afterDelay: 1)

    }else{
        self.tableView.endUpdates()
    }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...