【问题标题】:How do I show a UIActivityIndicator above the searchBar in UITableView如何在 UITableView 的 searchBar 上方显示 UIActivityIndi​​cator
【发布时间】:2013-10-01 13:08:34
【问题描述】:

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

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

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

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

- (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 的位置。

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

编辑:添加了显示发生了什么的屏幕截图

【问题讨论】:

    标签: uitableview uisearchbar uiactivityindicatorview


    【解决方案1】:

    为什么我一发布问题就找到了答案?无论如何,一周左右后我又回到了这个问题上,并认为我已经弄清楚了。让我知道是否有更好的方法。

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

    看起来的诀窍是确保您重新分配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;
        }
    

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      相关资源
      最近更新 更多