1

我已经花了数小时阅读几十个不同的博客和其他人q在这里关于这个,所以,但我陷入了僵局。UISearchDisplayController - 如何实现并使其工作

我的问题是你如何使这件事情的工作。我已经在IB和代码中试过了。

当我在IB中尝试它时,即使将它拖到表格视图(UITableViewController的)上,它也不会显示,并且它会“捕捉”到表头所在的表格中。但搜索栏永远不会显示。 (这是一个子问题,任何人都知道为什么?)

所以我试着在代码中设置。我得到它分配,分配委托和数据源,但我从来没有得到那个表示“没有结果”的表视图。酒吧出现了,我可以输入它,但它总是显示一张空桌子,即使我知道应该有结果。我怀疑它没有显示正确的表格,因为单元格显示“没有结果”文本。失败的

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil]; 
    results = [[NSMutableArray alloc] init]; 

    UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    sb.tintColor = [UIColor greenColor]; 
    searchTable.tableHeaderView = sb; 

    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self]; 
    [self setSearchDisplayController:sdc]; 
    [sdc setDelegate:self]; 
    [sdc setSearchResultsDataSource:self];} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.searchTable) { 
     // normal table view population 
     return [names count]; 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
     return [results count]; 
    } 
    return 0; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 

    if (tableView == self.searchTable) { 
     // normal table view population 
     cell.textLabel.text = [names objectAtIndex:indexPath.row]; 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
    } 

    cell.textLabel.textColor = [UIColor blueColor]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == self.searchTable) { 
     // normal table view population 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
    } 
} 


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller 
{ 
    [results removeAllObjects]; // First clear the filtered array. 

    /* 
    Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. 
    */ 
    for (NSString *str in names) 
    { 
     NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])]; 
     if (result == NSOrderedSame) 
     { 
      [results addObject:str]; 
     } 
    } 
} 

Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :). 
+0

如果没有看到一些代码,很难知道发生了什么问题。 – conmulligan 2011-04-21 22:51:50

回答

0

一对夫妇可能点我看:

1)你要为你的ViewController在其.h文件中的委托?例如:

@interface myViewController : UIViewController <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>{ 

2)我不知道,如果这是你的完整代码,或只是相关的片段,但如果它是完整的代码,有几个洞之前,你会看到任何数据显示出来。你需要在那里添加一个tableview来显示结果,并在你的cellForRowAtIndexPath方法中填写第二个if语句。

我建议您查看Apple的TableSearch的示例代码,这应该让您朝着正确的方向前进。

+0

我一直在关注这个示例,我无法弄清楚它们是如何工作的。我看着我在IB的关系,他们都完全一样。我确实在那里有那些代表。所以我必须从结果中创建表,searchController不会那样做? – sasquatch 2011-04-22 14:01:34

+0

从示例代码,它看起来像你没有把搜索结果放入表中if(tableView == self.searchDisplayController.searchResultsTableView)cell.textLabel.text = [results objectAtIndex:indexPath.row]; }' – Sam 2011-04-22 17:08:53

+1

因此,如果我必须构建该表,那么使用searchDisplayController有什么意义。我的意思是如果我必须做所有这些,它有什么好处,只是很酷的动画? – sasquatch 2011-04-22 20:48:07

相关问题