如何在UITableView中的搜索栏上方显示UIActivityIndicator

如何在UITableView中的搜索栏上方显示UIActivityIndicator,uitableview,uisearchbar,uiactivityindicatorview,Uitableview,Uisearchbar,Uiactivityindicatorview,我试图找出正确的方法是在UITableView(分组)的顶部添加搜索栏和活动指示器。搜索可能需要几秒钟才能完成,我希望activityIndicator显示在搜索栏上方的一行中,并在搜索完成后消失。搜索当前在后台执行 我的方法是在IB中创建一个UIView作为tableHeaderView,然后在viewDidLoad中创建一个UISearchBar,并将其作为子视图添加到tableHeaderView中。这很好,我也可以在IB中这样做 然后,当用户选择搜索栏时,我将隐藏导航栏并在搜索栏中显示取

我试图找出正确的方法是在UITableView(分组)的顶部添加搜索栏和活动指示器。搜索可能需要几秒钟才能完成,我希望activityIndicator显示在搜索栏上方的一行中,并在搜索完成后消失。搜索当前在后台执行

我的方法是在IB中创建一个UIView作为tableHeaderView,然后在viewDidLoad中创建一个UISearchBar,并将其作为子视图添加到tableHeaderView中。这很好,我也可以在IB中这样做

然后,当用户选择搜索栏时,我将隐藏导航栏并在搜索栏中显示取消按钮

当用户单击搜索按钮时,我将执行以下操作:

- (void)showSearchActivityIndicator:(NSString*)title {
    _activityView = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
    [UIView beginAnimations:nil context: NULL];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    _activityView.view.frame = CGRectMake(0, 0, 320, 44);
    _searchBar.frame = CGRectMake(0, 44, 320, 44);

    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, 320, 88);
    [self.tableView.tableHeaderView addSubview: _activityView.view];
    //[self.tableView setContentOffset:CGPointMake(0, -44)];
    [_activityView show:title];

    [UIView commitAnimations];

}
请注意,ActivityViewController视图有一个UIView,其中UIActivityView和UILabel作为子视图

这里的问题是,当添加ActivityViewController视图时,我似乎不知道如何使tableview内容本身向下滚动。当tableViewHeader的高度改变时,我本来希望tableView能够自我调整,但这似乎没有发生

使用setContentOffset似乎也会影响tableViewHeader的位置

我在这里遗漏了什么,还是我的整个方法都错了

编辑:添加显示所发生情况的屏幕截图
为什么我一发布问题就找到了答案?不管怎么说,大约一个星期后我又回到这个话题,我想我已经明白了。如果有更好的办法,请告诉我

多亏了另一篇文章,这似乎对我有效,我希望它对其他人有用——但仍然不确定这是否是最好的方式

它的诀窍似乎是确保重新分配
tableView.tableHeaderView
,并在
[beginUpate][endUpdate]
中包装任何更改。如下图所示,我复制headerView,更改其高度,添加ActivityIndicator,然后设置
tableView.tableHeaderView=view

对格式错误表示歉意

 - (void)showSearchActivityIndicator:(NSString*)title {
        _activityView = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
        UIView *view = self.tableView.tableHeaderView;
        [UIView beginAnimations:nil context: NULL];
        [UIView setAnimationDuration:0.25];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

        float width = self.tableView.frame.size.width;

        _activityView.view.frame = CGRectMake(0, 0, width, 44);
        _searchBar.frame = CGRectMake(0, 44, width, 44);
        [self.tableView beginUpdates];
        view.frame = CGRectMake(0, 0, width, 88);
        [view addSubview: _activityView.view];
        self.tableView.tableHeaderView = view;
        [_activityView show:title];
        [self.tableView endUpdates];
        [UIView commitAnimations];

    }
    - (void)removeSearchActivityIndicator {
        UIView *view = self.tableView.tableHeaderView;
        float width = self.tableView.frame.size.width;
        [UIView beginAnimations:nil context: NULL];
        [UIView setAnimationDuration:0.25];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [self.tableView beginUpdates];
        [_activityView.view removeFromSuperview];
        view.frame = CGRectMake(0, 0, width, 44);
        _searchBar.frame = CGRectMake(0, 0, width, 44);
        self.tableView.tableHeaderView = view;
        [self.tableView endUpdates];
         [UIView commitAnimations];
        _activityView = nil;
    }