0

我已經基於我的代碼this示例。初始數據顯示在表格視圖中,但我不確定如何執行其餘部分以顯示過濾的數據。我沒有錯誤,搜索欄有效,但沒有過濾數據。這裏是我的plist XML:客觀c過濾器表視圖與來自plist的數據

<dict> 
    <key>Things to do</key> 
    <array> 
     <dict> 
      <key>activity</key> 
      <string>Watch movies or TV shows</string> 
      <key>keywords</key> 
      <array> 
       <string>tv shows</string> 
       <string>movies</string> 
      </array> 
     </dict> 
     <dict> 
      <key>activity</key> 
      <string>Go Hiking</string> 
      <key>keywords</key> 
      <array> 
       <string>hiking</string> 
       <string>mountains</string> 
      </array> 
     </dict> 
     <dict> 
      <key>activity</key> 
      <string>Video Games</string> 
      <key>keywords</key> 
      <array> 
       <string>video games</string> 
       <string>playstation</string> 
       <string>xbox</string> 
       <string>nintendo</string> 
      </array> 
     </dict> 
    </array> 
</dict> 

這裏是我的CategoriesViewController.m:

@interface CategoriesViewController() <UISearchDisplayDelegate> 

@property (nonatomic, strong) NSMutableArray *tableData; 
@property (nonatomic, strong) NSMutableArray *searchResults; 
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 

@end 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"]; 
NSDictionary *categoriesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 

self.tableData = categoriesDictionary[@"Things to do"]; 
//I think this next line is supposed to make it appear on top of the original table 
self.searchResults = [NSMutableArray arrayWithCapacity:[self.tableData count]]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; 
    } else { 
     cell.textLabel.text = self.tableData[indexPath.row][@"activity"]; 
    } 

    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.searchResults removeAllObjects]; 

    NSPredicate *resultPredicate = [NSPredicate 
    predicateWithFormat:@"SELF contains[cd] %@", 
    searchText]; 

    self.searchResults = [NSMutableArray arrayWithArray:[self.tableData filteredArrayUsingPredicate:resultPredicate]]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
    scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
    objectAtIndex:[self.searchDisplayController.searchBar 
    selectedScopeButtonIndex]]]; 
    return YES; 
} 

這就像例子中,除了我改變了我的地方數據的來源。我如何根據關鍵字進行搜索?在這個例子中,我只搜索我的初始表視圖中包含的內容嗎?感謝您的時間...

+0

其中你實際上重新加載tableview – CoolMonster

回答

1

很難說沒有更多的代碼,但我認爲你需要根據搜索字符串顯示/隱藏你的兩個表視圖。

如果字符串爲空,這將隱藏您的searchResultsTableView並顯示您的其他表格視圖,我正在親切地呼叫whatEverYouCalledYourOtherTableView

如果你的字符串不爲空,你做搜索,顯示searchResultsTableView,隱藏whatEverYouCalledYourOtherTableView並重新加載數據爲searchResultsTableView

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    if ([searchString isEqualToString:@""]) 
    { 
     self.searchDisplayController.searchResultsTableView.hidden = YES; 
     self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = NO; 
    } 
    else 
    { 
     [self filterContentForSearchText:searchString 
            scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
          objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 


     self.searchDisplayController.searchResultsTableView.hidden = NO; 
     self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = YES; 
     [self.searchDisplayController.searchResultsTableView reloadData]; 
    } 
    return YES; 
} 

另外你的filterContentForSearchText是錯誤的。你有一個不是字符串的字典數組。首先抓住字符串,然後運行搜索。你最好馬上創建一個字符串字典,讓你的搜索工作最簡單的方法就是這樣。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.searchResults removeAllObjects]; 

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; 

    NSMutableArray *activityArray = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < [self.tableData count]; i++) 
    { 
     [activityArray addObject:self.tableData[i][@"activity"]]; 
    } 

    self.searchResults = [NSMutableArray arrayWithArray:[activityArray filteredArrayUsingPredicate:resultPredicate]]; 
} 
+0

我認爲基於這個例子,過濾表應該顯示在原來的頂部。我會更新我的代碼,你能否看到這是否有所作爲? – denikov

+0

我沒有看過這個例子,但我認爲這個想法是你有兩個表在同一個地方,但當然只有一個顯示在一個時間。因此,當用戶搜索(或清除搜索)時,您需要隱藏一個並顯示另一個,然後爲您搜索表格重新加載數據。完整列表不會改變,所以不需要重新加載數據。合理? – ansible

+0

是的,這是有道理的。我想我必須重寫代碼。根據我添加的代碼,這裏沒有修復? – denikov

相關問題