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