搜索&找到一个短语,得到它之前和之后的两个单词

时间:2017-08-24 13:16:19

标签: nsstring nsarray uisearchbar nsrange

我正在使用以下代码在数组中搜索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数组中。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

所以我编写了这段代码,只是把搜索到的短语放到上下文中,如果它接近段落的末尾(在数组索引中的段落)那么它会显示“少数”字在tableview单元格之前,如果它不接近结尾,则“少数”单词会在后显示

-(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大致相同(虽然可以使它更准确)。