的iPad:在酥料饼

的iPad:在酥料饼

问题描述:

我想要实现这个的iPad:在酥料饼

1)当用户开始在文本框一酥料饼闪烁打字和预测搜索结果显示,项目在酥料饼表视图列表按以字符串形式输入文本域。

2)此外,这些数据应该随着输入的每个新字母进行刷新。

一种预测性搜索。

请帮助我,并建议可能的方法来实现这一点。

这听起来像你已经有一个相当不错的主意。我的建议是在顶部的搜索栏中显示一个UITableView,然后使用搜索词简单地驱动表视图的数据源,并在每次用户键入框时在表视图上调用reloadData

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