2017-08-24 20 views
0

次のコードを使用して、配列内でUISearchBarに入力されたフレーズを検索し、フレーズがあったインデックスのコンテンツを追加しますテーブルビューに表示される配列に含まれています。フレーズを検索して検索し、その前後にある2つの単語を取得します

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)enteredSearchText{ 


    if ([enteredSearchText length] == 0) { 

     [wholeTextOfDoaasLive removeAllObjects]; //wholeTextOfDoaasLive is an NSMutableArray 

    } 
    else{ 

     [wholeTextOfDoaasLive removeAllObjects]; 

     for (NSString * string in AllWholeText) {//AllWholeText is an NSMutableArray 

      NSRange r = [string rangeOfString:enteredSearchText options:NSCaseInsensitiveSearch]; 

      if (r.location != NSNotFound){ 

       [wholeTextOfDoaasLive addObject:string]; 
      } 
     } 
    } 

    [_searchResultsTable reloadData]; 
} 

しかし、AllWholeText配列のすべてのインデックスは、大量のテキストが含まれていると私はwholeTextOfDoaasLive配列に(検索フレーズがで見つかったことを)全体のオブジェクトを追加する必要はありません。その代わりに、見つかったフレーズと2つの単語の前後を、私のwholeTextOfDoaasLive配列に追加したいだけです。どうやってやるの?

+0

私はそれが簡単な問題ではないと思います。より簡単な解決策は、 "upTo10WordsBefore_SearchedText_upTo10WordsAfter"の代わりに "... upTo10CharBefore_SearchedText_upTo10CharAfter ..."を表示することです。複雑な部分の1つは、 'enteredSearchText'が" i the "で、" Hi There "のような文を持っていれば(それはもっと多くの単語を組み合わせるなどです) – Larme

答えて

0

私は、段落の終わり近く(段落が配列のインデックス内にある)であれば、検索されたフレーズを文脈に置くだけで、「数」の単語で表示されます。テーブルビューのセルにがあります。それが終わり近くにない場合は、の後に「ほとんど」の単語が表示されません。

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)enteredSearchText{ 

    if ([enteredSearchText length] == 0) { 

     [wholeTextOfDoaasLive removeAllObjects]; 

    } 
    else{ 

     [wholeTextOfDoaasLive removeAllObjects]; 

     for (NSString * string in AllWholeText) { //create string that holds the value in that index 

      NSRange r = [string rangeOfString:enteredSearchText options:NSCaseInsensitiveSearch]; 

      NSUInteger indexOfFoundString = r.location; 
      NSUInteger numberOfAvailableCharactersAfterPhrase = string.length - (indexOfFoundString + 1); 

      NSUInteger takeCharacters = 50; //number of characters that will show in tableview cell 
      foundStringIsAtTheEnd =NO; 

      /// START: eliminate the "out of bounds" error /// 
      // if the searched phrase is at the end of the text and you ask for 50 characters after it, the app to crash. So here we check if the searched phrase is the near the end or if it's far away from the end. A) if it's close to the end then show words before it. B) if it's far from the end then show words after it. 

      if (numberOfAvailableCharactersAfterPhrase < 50) { 
       takeCharacters = 40; 

       if (numberOfAvailableCharactersAfterPhrase < 40) { 
        takeCharacters = 30; 

        if (numberOfAvailableCharactersAfterPhrase < 30) { 
         takeCharacters = 20; 

         if (numberOfAvailableCharactersAfterPhrase < 20) { //there is less than 20 characters after the searched phrase 

          takeCharacters = numberOfAvailableCharactersAfterPhrase+30; 
          foundStringIsAtTheEnd =YES; 

         } 

        } 
       } 
      } 
      /// END: eliminate the "out of bounds" error /// 

      if (indexOfFoundString != NSNotFound){ 

       NSString *tableViewString; 

       if (foundStringIsAtTheEnd == YES) { 

        indexOfFoundString -= 30; //to show few words before the searched phrase 

        tableViewString = [string substringWithRange:NSMakeRange(indexOfFoundString, takeCharacters)]; 

        /// START: remove cropped words, we want only full words /// 
        NSArray *arrayStoredText = [tableViewString componentsSeparatedByString:@" "]; 
        NSMutableArray *myMutable = [[NSMutableArray alloc]initWithArray:arrayStoredText]; 
        [myMutable removeObjectAtIndex:0]; //cuz maybe it's a cropped word 
        tableViewString = @""; 
        tableViewString = [myMutable componentsJoinedByString:@" "]; 
        /// END: remove cropped words, we want only full words /// 


       } 
       else{ //if searched phrase is far from the end 

        tableViewString = [string substringWithRange:NSMakeRange(indexOfFoundString, takeCharacters)]; 

        NSArray *arrayStoredText = [tableViewString componentsSeparatedByString:@" "]; 
        NSMutableArray *myMutable = [[NSMutableArray alloc]initWithArray:arrayStoredText]; 
        [myMutable removeLastObject]; //cuz maybe it's a cropped word 
        tableViewString = @""; 
        tableViewString = [myMutable componentsJoinedByString:@" "]; 

       } 

       [wholeTextOfDoaasLiveArray addObject:tableViewString]; 
      } 
     } 
    } 

    [_searchResultsTable reloadData]; 
} 

それは私がもともとのために(代わりに「数」の2つの単語を)尋ねたかを正確に行いませんが、私はこれは私がテキストの量は、フレーズの前に示すことを確実にこのよう以来、ユーザーのための優れていると思いますテーブルビューの各セルではほぼ同じです(より正確にすることもできます)。

関連する問題