将搜索放大镜更改为UIActivityIndi​​catorView

时间:2011-02-24 19:08:56

标签: uisearchbar uiactivityindicatorview

我有一个应用程序,可搜索13,000个单元格中的文本。我知道这是很多细胞。在较旧的iphone上,搜索需要几秒钟,因此我想提供一个指示器视图,向用户显示应用程序仍在运行。我想出了将UISearchBar放大镜改为UIActivityIndi​​catorView的想法。代码在模拟器中工作,但是当我在旧的ipod touch上测试时,微调器不显示。它实际上确实显示,但只有在搜索完成后才会显示。知道为什么?这是代码。

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
UIActivityIndicatorView *spin = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGRect spinnerFrame = CGRectMake(12.0, 12.0, 20.0, 20.0);
spin.frame = spinnerFrame;
spin.clipsToBounds = YES;
spin.backgroundColor = [UIColor whiteColor];
[searchBar addSubview:spin];
[spin startAnimating];
[self performSelectorOnMainThread:@selector(filterContentForSearchText:) withObject:searchBar.text waitUntilDone:YES];
[spin stopAnimating];
[spin removeFromSuperview];
//[self filterContentForSearchText:searchBar.text];
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
[self.searchDisplayController.searchResultsTableView reloadData];

}

1 个答案:

答案 0 :(得分:1)

将搜索栏替换为:

@interface _SearchBarWithSpinner : UISearchBar
{
    UIActivityIndicatorView *_spinnerView;
    UIView                  *_searchIconView;
    UITextField             *_internalTextField;
}

- (void)showSpinner;
- (void)hideSpinner;

@end

@implementation _SearchBarWithSpinner

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldDidBeginEditingNotification:)
                                                     name:UITextFieldTextDidBeginEditingNotification
                                                   object:nil];
    }

    return self;
}

- (void)showSpinner
{
    if(_internalTextField)
    {
        if(_spinnerView == nil)
            _spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        [_internalTextField setLeftView:_spinnerView];
        [_spinnerView startAnimating];
    }
}

- (void)hideSpinner
{
    [_spinnerView stopAnimating];
    [_internalTextField setLeftView:_searchIconView];
}

#pragma mark - Private

- (void)textFieldDidBeginEditingNotification:(NSNotification *)notification
{
    if(_internalTextField == nil)
    {
        UITextField *editedTextField    = notification.object;
        UIView      *superView          = editedTextField.superview;

        while(superView && superView != self)
            superView = superView.superview;

        if(superView == self)
        {
            _internalTextField  = editedTextField;
            _searchIconView     = _internalTextField.leftView;
        }
    }
}

@end