如何在UITableView中的搜索栏上方显示活动指示器

时间:2013-10-01 13:08:35

标签: uitableview uisearchbar uiactivityindicatorview

我试图找出将searchBar和activityIndi​​cator添加到UITableView(已分组)顶部的正确方法。搜索可能需要几秒钟才能完成,我希望activityIndi​​cator出现在searchBar上方的一行中,并在搜索完成后消失。搜索目前在后台执行。

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

然后当用户选择searchBar时,我隐藏了navigationBar并在searchBar中显示了Cancel按钮。

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

- (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视图有一个带有UIActivityView的UIView和一个UILabel作为子视图。

这里的问题是,当添加ActivityViewController视图时,我似乎无法弄清楚如何使tableview内容向下滚动。我希望tableView在tableViewHeader高度改变时自行调整,但这似乎不会发生。

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

我在这里缺少什么,或者我的整个方法是错的?

编辑:添加屏幕截图,显示发生的情况 enter image description here

1 个答案:

答案 0 :(得分:0)

为什么我发布一个问题后才能找到答案呢?无论如何,我在一周左右之后回到了这里,并认为我已经弄明白了。如果有更好的方法,请告诉我。

感谢另一篇文章,这似乎对我有用,我希望它对其他人有用 - 但仍然不确定它是否是最佳方式。

看来诀窍是确保重新分配tableView.tableHeaderView并包装[beginUpate] [endUpdate]中的所有更改。如下所示,我复制headerView,更改其高度,添加ActivityIndi​​cator,然后设置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;
    }

enter image description here