2011-02-24 74 views
2

我有一個應用程序,通過13,000個單元格搜索文本。我知道這是很多細胞。在較舊的iPhone上,搜索需要幾秒鐘的時間,因此我想提供一個指示器視圖,向用戶顯示該應用程序仍在工作。我想出了將UISearchBar放大鏡更改爲UIActivityIndi​​catorView的想法。該代碼在模擬器中工作,但是當我在舊的iPod touch上測試時,微調框不顯示。它實際上確實顯示,但只有在搜索完成後才顯示。任何想法爲什麼?這是代碼。更改搜索放大鏡到UIActivityIndi​​catorView

- (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

這個替換您的搜索欄:

@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 
相關問題