2012-05-31 4 views
0

아래 제품을 검색하고 있습니다. 그것은 잘 작동하지만 2 단어 사이를 검색 할 때 사용자가 다음 단어의 첫 번째 문자를 입력하면 결과가 사라집니다. 즉 "td pro"또는 "pro td"를 검색하면 결과가 나타납니다. 이처럼 검색하면 td (결과가 있음) td (공백) 결과가 없습니다 (결과가 있습니다). 사용자가 제품을 검색 할 수 없기 때문에 componentsSeperatedByString에서 단어를 공백으로 구분해야합니다. 어떤 특정 순서로. 더 좋은 방법이 없다면?디스플레이 컨트롤러 검색 iOS 결과가 사라지고 단어 사이에 다시 나타납니다.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 

{

[self.filteredListContent removeAllObjects]; // First clear the filtered array. 



for (XMLStringFile *new in rssOutputData_MutableArray) 
{ 

    //Product scope 
    if ([scope isEqualToString:@"Product"]) 
    { 
     // Split into search text into separate "words" 
     NSArray * searchComponents = [searchText componentsSeparatedByString: @" "]; 

     ; 
     BOOL foundSearchText = YES; 

     // Check each word to see if it was found 
     for (NSString * searchComponent in searchComponents) { 
      NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 
      NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 

      foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound); 
     } 

     // If all search words found, add to the array 
     if (foundSearchText) 
     { 
      [self.filteredListContent addObject: new]; 
     } 
    } 

} 

}

많은 감사

답변

0

누군가가 나에게 도움이 또 다른 스택 질문의 도움으로 그것을 고정 : 여기

내 코드입니다 .

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 

{

[self.filteredListContent removeAllObjects]; // First clear the filtered array. 



for (XMLStringFile *new in rssOutputData_MutableArray) 
{ 

    //Product scope 
    if ([scope isEqualToString:@"Product"]) 
    { 
     // Split into search text into separate "words" 
     NSArray * searchComponents = [searchText componentsSeparatedByString: @" "]; 

     //Before searching trim off the whitespace 
     searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; 


     BOOL foundSearchText = YES; 

     // Check each word to see if it was found 
     for (NSString * searchComponent in searchComponents) { 
      NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 
      NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 

      foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound); 
     } 

     // If all search words found, add to the array 
     if (foundSearchText) 
     { 
      [self.filteredListContent addObject: new]; 
     } 
    } 

} 

}는

선정 된 광고는 다음

// 공백 잘라내 검색하기 전에 검색 텍스트 = 검색 텍스트 stringByTrimmingCharactersInSet [NSCharacterSet whitespaceCharacterSet];

관련 문제