2011-03-02 92 views
1

我想要實現這個的iPad:在酥料餅

1)當用戶開始在文本框一酥料餅閃爍打字和預測搜索結果顯示,項目在酥料餅表視圖列表按以字符串形式輸入文本域。

2)此外,這些數據應該隨着輸入的每個新字母進行刷新。

一種預測性搜索。

請幫助我,並建議可能的方法來實現這一點。

回答

1

這聽起來像你已經有一個相當不錯的主意。我的建議是在頂部的搜索欄中顯示一個UITableView,然後使用搜索詞簡單地驅動表視圖的數據源,並在每次用戶鍵入框時在表視圖上調用reloadData

7

UISearchDisplayController爲你做了大部分的繁重工作。

在您的視圖中放置一個UISearchBar(不是UITextField),並將UISearchDisplayController連接到它。

// ProductViewController.h 
@property IBOutlet UISearchBar *searchBar; 
@property ProductSearchController *searchController; 

// ProductViewController.m 
- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 

    searchBar.placeholder = @"Search products"; 
    searchBar.showsCancelButton = YES; 

    self.searchController = [[[ProductSearchController alloc] 
            initWithSearchBar:searchBar 
            contentsController:self] autorelease]; 
} 

我通常的子類UISearchDisplayController並將它是它自己的代表,searchResultsDataSource和searchResultsDelegate。後兩者以正常方式管理結果表。

// ProductSearchController.h 
@interface ProductSearchController : UISearchDisplayController 
      <UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource> 

// ProductSearchController.m 
- (id)initWithSearchBar:(UISearchBar *)searchBar 
       contentsController:(UIViewController *)viewController 
{ 
    self = [super initWithSearchBar:searchBar contentsController:viewController]; 

    self.contents = [[NSMutableArray new] autorelease]; 
    self.delegate = self; 
    self.searchResultsDataSource = self; 
    self.searchResultsDelegate = self; 

    return self; 
} 

搜索欄中的每個按鍵都會調用searchDisplayController:shouldReloadTableForSearchString:。快速搜索可以直接在這裏執行。

- (BOOL) searchDisplayController:(UISearchDisplayController*)controller 
      shouldReloadTableForSearchString:(NSString*)searchString 
{ 
    // perform search and update self.contents (on main thread) 
    return YES; 
} 

如果你的搜索可能需要一些時間,做到與NSOperationQueue背景。在我的示例中,ProductSearchOperation將在其完成時調用​​。

// ProductSearchController.h 
@property INSOperationQueue *searchQueue; 

// ProductSearchController.m 
- (BOOL) searchDisplayController:(UISearchDisplayController*)controller 
     shouldReloadTableForSearchString:(NSString*)searchString 
{ 
    if (!searchQueue) { 
     self.searchQueue = [[NSOperationQueue new] autorelease]; 
     searchQueue.maxConcurrentOperationCount = 1; 
    } 

    [searchQueue cancelAllOperations]; 
    NSInvocationOperation *op = [[[ProductSearchOperation alloc] 
      initWithController:self 
      searchTerm:searchString] autorelease]; 
    [searchQueue addOperation:op]; 

    return NO; 
} 

- (void) showSearchResult:(NSMutableArray*)result 
{ 
    self.contents = result; 
    [self.searchResultsTableView 
      performSelectorOnMainThread:@selector(reloadData) 
      withObject:nil waitUntilDone:NO]; 
} 
+0

在界面生成器,一個UISearchBar似乎風格的出現(如命名)一個工具欄,而作爲被的UITextField樣式好看在空白頁上。有沒有辦法讓UISearchBar不需要在「欄」中 - 例如,如果你想在頁面上有多個啓用搜索的文本框? – radven 2012-05-29 01:47:41

+0

@radven你應該問,作爲網站上的頂級問題。 – 2012-05-29 02:29:38

+0

完成 - 張貼在這裏:http://stackoverflow.com/questions/10792578/making-a-uisearchbar-a-drop-in-replacement-for-a-uitextfield – radven 2012-05-29 02:56:40