2011-11-17 80 views
1

目前我填充我的tableviewcells與多個陣列的代表名稱,ID等SearchDisplayController搜索多個陣列

我的問題涉及的內容,當我開始使用搜索顯示控制器。我有一個包含名稱列表,ID列表,條形碼列表和別名列表的數組。當用戶鍵入搜索欄時,我需要能夠搜索所有4個數組。當它發現在1個陣列它具有配對的結果與3個其他陣列的結果..

  1. 名稱(蘋果,胡蘿蔔,香蕉,狗)
  2. 別名(紅,橙,黃色,棕色)
  3. 條形碼(1,2,10,20)
  4. ID(30,40,50,60)

因此,如果用戶鍵入 「一個」 我應該填充表查看 蘋果,胡蘿蔔,香蕉和相關的別名,條碼,身份證。

如果用戶輸入2我只能得到 胡蘿蔔和狗。

如果用戶輸入0,我會得到所有這些項目。

任何想法如何做到這一點?

更新: 這就是我做到的。

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 

    BOOL shouldReturn = FALSE; 
    [searchResults removeAllObjects]; 

    for (int i = 0; i < [itemIDRows count]; i++) { 

     BOOL foundResult = FALSE; 

     if ([[itemIDRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) { 
      foundResult = TRUE; 
     } 
     if ([[nameRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) { 
      foundResult = TRUE; 
     } 
     if ([[barcodeRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) { 
      foundResult = TRUE; 
     } 
     if ([[aliasRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) { 
      foundResult = TRUE; 
     } 
     if (foundResult) { 
      NSNumber *result = [NSNumber numberWithInt:i]; 
      if ([self searchResults] == nil) { 
       NSMutableArray *array = [[NSMutableArray alloc] init]; 
       [self setSearchResults:array]; 
       [array release]; 
      } 
      [searchResults addObject:result]; 
      shouldReturn = YES; 
     } 
    } 
    return shouldReturn;  
} 

然後當我填充的tableview我做這樣的事

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) { 
    [cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]]; 
} else { 
    [cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:indexPath.row]]; 
} 

然而,當我鍵入類似9999它帶來了情況,其中只有1個9是在ID或條形碼。任何想法如何解決這個問題?

UPDATE2: 通過讓列表總是刷新來解決問題,而不是僅在找到結果時重新加載數據。現在,它完美的作品:d

回答

0

搜索顯示控制器調用

UISearchDisplayDelegate 

方法:

searchDisplayController:shouldReloadTableForSearchString: 

在此方法中,你需要實現你的邏輯。這個邏輯將需要搜索所有4個陣列的命中,並進行適當的查找(即從橙色到胡蘿蔔,或從50到香蕉)。每次你得到一個命中,我會把它放在一個NSMutableSet(以防止愚蠢)。然後,當您完成搜索所有數組時,將該數據集複製到您的表的數據源讀取的數組中。

如果你想要顯示用戶爲什麼給定的行是一個命中(即他們鍵入50並獲得香蕉),你必須在你的表格單元格中顯示所有4個屬性。你需要突出顯示匹配的部分。如果你這樣做,我會創建一個小容器類,就像包含所有4個屬性的「searchHit」,還有一個屬性獲得命中的標誌,以及可能獲得命中的屬性的子字符串(so例如,您可以爲此子字符串使用黃色背景。)然後,tableView的數據源將顯示一組要顯示的searchHit對象,並且您的cellForRowAtIndexPath將需要對此對象進行解碼並適當地顯示匹配。

0

您可以使用KVC對象與NSPredicate做到這一點。

創建一個NSObject響應KVC方案http://theocacao.com/document.page/161。你可以使用財產。

與NSPredicate http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name LIKE[cd] %@ OR self.alias LIKE[cd] %@",searchString,searchString]; 
NSArray *result = [baseArray filteredArrayUsingPredicate:predicate]; 
過濾您的陣列