2012-07-06 4 views
2

오랫동안 검색하고, 주저하고 더 많은 검색을 한 후에 나는 이것을 알아낼 수 없습니다.SearchDisplayController 여러 배열로 검색

저는 SearchDisplayController가 있습니다. 각 UITableViewCell이 4 개의 문자열 (제목, 설명 등)으로 이루어지기 때문에 NSPredicate를 사용하여 검색 할 곳이 4 개의 다른 배열을 가지고 있습니다.

내가 지금 가지고있는 다음과 같은 가득 4 검색 배열, search_title, SEARCH_DESCRIPTION 등 :

- (void)filterContentForSearchText:(NSString*)searchText 
          scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"SELF contains[cd] %@", 
            searchText]; 

    self.search_category = [self.temp_category filteredArrayUsingPredicate:resultPredicate]; 
    self.search_producttitle = [self.price_producttitle filteredArrayUsingPredicate:resultPredicate]; 
    self.search_type = [self.price_type filteredArrayUsingPredicate:resultPredicate]; 
    self.search_description = [self.price_description filteredArrayUsingPredicate:resultPredicate]; 

내가 NSLog (배열 회수)를 사용하는 경우, 나는 그것이 정확한 수를 제공하기 때문에이 작동하고 볼 수 있습니다 검색어를 입력 할 때마다 각 배열을 찾습니다.

내있는 tableview 셀은 다음과 같습니다

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.nameLabel.text = [self.search_producttitle objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]; 
     cell.priceLabel.text = [self.search_price objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]; 
     cell.descrLabel.text = [self.search_description objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]; 
     cell.IDLabel.text = [self.search_type objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]; 
    } 
    else{ 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.nameLabel.text = [price_producttitle objectAtIndex:indexPath.row]; 
     cell.IDLabel.text = [price_type objectAtIndex:indexPath.row]; 
     cell.priceLabel.text = [price objectAtIndex:indexPath.row]; 
     cell.descrLabel.text = [price_description objectAtIndex:indexPath.row]; 

어쩌면 당신은 이미 내가 할 노력하고있어 볼 수 있습니다. 이제 검색 배열이라고 부르는 기존 배열에서 새 배열을 만들고 있습니다. 그러나 이러한 배열은 서로 독립적입니다 (한 배열은 비어있는 반면 다른 배열은 연구 결과로 채워질 수 있음). 나는 그 색인이 어디 있는지 알 필요가있다. 그것은 그 searcharrays에 저장되어있다. search_producttitle의 데이터가 price_producttitle (원래 배열)의 인덱스 1,3 및 4에서 나온다는 것을 알고 있다면 indexPath.row에 해당 숫자를 사용하여 표시 할 수 있습니다. 적어도 지금은 내 계획이며 셀을 만들 때 objectAtIndex에 사용해야하는 숫자가 들어있는 searchResult 배열을 만드는 것입니다.

저는이 작업에 어려움을 겪고 있습니다. 셀이 여러 배열로 구성되어 있기 때문에 여러 배열 내에서 검색하는 예제를 찾을 수 없습니다.

누군가 좋은 방향으로 나에게 유용한 힌트를 제공 할 수 있습니까?

미리 감사드립니다.

Prastow

참조 내가 사용 : 아무도 내 질문에 대답하지 않지만

SearchDisplayController search multiple arrays

http://ygamretuta.me/2011/08/10/ios-implementing-a-basic-search-uisearchdisplaycontroller-and-interface-builder/

답변

1

, 나는 나 자신을 알아 냈다.

사실, 너무 어렵다고 생각했습니다. 내 경우에는 필요하지 않은 NSPredicate를 사용하고 싶었다. 먼저 NSPredicate에서 문자열을 가져 와서 문자열을 비교하려고했습니다. 이제 모든 objectatindex에 대한 루프에서 rangeofstring을 사용합니다. 모든 배열에서이 작업을 수행합니다.

어레이에서 발견되면 YES를보고 인덱스가 다른 배열에 저장됩니다. 이 배열은 나중에 UITableView를 만들 때 사용되며 indexPath.row를 대체합니다.

[self.searchResults removeAllObjects]; 

for (int i = 0; i < [temp_category count]; i++) { 
    BOOL foundResult = FALSE; 

    if ([[temp_category objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) { 
     foundResult = TRUE; 
    } 
    if ([[price_producttitle objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) { 
     foundResult = TRUE; 
    } 
    if ([[price_type objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) { 
     foundResult = TRUE; 
    } 
    if ([[price_description objectAtIndex:i] rangeOfString:searchText].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]; 

    } 
} 

NSLog (@"array = %i", [searchResults count]); 
NSLog(@"%@",searchResults); 

이 답변에는 크레딧이 적용되지 않습니다. 크레딧은 다음으로 이동해야합니다. SearchDisplayController search multiple arrays

이 방법을 사용하여 - (void) searchtableview에 구현했습니다. 이것에 관해 많은 질문이 없다는 것을 언급해야하며, 이것이 내가 찾을 수있는 유일한 유일한 대답이었습니다.

다른 사람들도이 기능을 유용하게 사용할 수 있기를 바랍니다.

0
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText 
{ 
if ([searchText length]==0) 
    { 
     temp_array1 =[array_Main1 mutableCopy]; 
     temp_array2 =[array_Main2 mutableCopy]; 
     temp_array3 =[array_Main3 mutableCopy]; 
    } 
else 
    { 
     [temp_array1 removeAllObjects]; 
     [temp_array2 removeAllObjects]; 
     [temp_array3 removeAllObjects]; 
     int g = 0; 
     for (int i=0; i< array_Main1.count;i++) 
     { 
      NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if (Range1.location != NSNotFound || Range2.location != NSNotFound || Range3.location != NSNotFound) 
      { 
       [temp_array1 addObject:[array_Main1 objectAtIndex:g]]; 
       [temp_array2 addObject:[array_Main2 objectAtIndex:g]]; 
       [temp_array3 addObject:[array_Main3 objectAtIndex:g]]; 

      } 
      g++; 
     } 
    } 
[table reloadData]; 
}