2017-08-24 104 views
0

我使用以下代碼在數組中搜索在UISearchBar中鍵入的短語,然後添加該短語所在的索引的內容找到將顯示在表視圖中的數組。搜索並找到一個短語並獲取它之前和之後的兩個單詞

-(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陣列。相反,我只想在它的前後添加一個找到的短語加兩個單詞到我的wholeTextOfDoaasLive數組中。我怎樣才能做到這一點?

+0

我不認爲這是一個簡單的問題。更簡單的解決方案是顯示:「... upTo10CharBefore_SearchedText_upTo10CharAfter ...」,而不是「upToToWordsBefore_SearchedText_upTo10WordsAfter」。其中一個複雜的部分是,如果'enteredSearchText'是「我」,並且你有像「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]; 
} 

它不會做什麼我原來問(兩個詞,而不是「少數」),但我認爲這是因爲對於這樣我保證文本的數量短語前顯示的用戶提供更好的在tableview的每個單元格中大致相同(可以使它更準確)。

相關問題