Objective-C hashtag / tags自动完成实现

时间:2014-04-05 20:55:37

标签: ios objective-c tags hashtag

我正在考虑使用Objective-C实现hashtag autocomplete,如图所示

enter image description here

我发现它比预期的有点困难。我正在寻找更具体的添加和删除主题标签的实现。例如,应立即删除主题标签。我想知道是否有人有类似的经验实现它,以及是否有更有效的方式实现它。感谢

1 个答案:

答案 0 :(得分:0)

我最后编写了一些我觉得有点难看的功能,但它确实有效。也许有一些更有效的方法来实现它。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //user is a singleton instance
    User *user = [User sharedUser];        
    user.autocompleteTableView.hidden = NO;

    int identifiedTagsStringLength = [self.identifiedTagsString length];
    int cursorLocation = range.location;

    //insert characters
    if (range.location >= identifiedTagsStringLength) {

        NSString *newSearch =@"";
        NSRange newRange;
        if(identifiedTagsStringLength != 0) {
            newSearch = [urlField.text substringFromIndex:identifiedTagsStringLength];
            newRange = NSMakeRange(range.location - identifiedTagsStringLength, 0);
        }
        else {
            newSearch = textField.text;
            newRange = range;
        }

        NSString *substring = [NSString stringWithString:newSearch];
        substring = [substring stringByReplacingCharactersInRange:newRange withString:string];
        [self searchAutocompleteEntriesWithSubstring:substring];

        if (cursorLocation > currentTagsRange) {
            currentTagsRange = cursorLocation;
        }
    }
    //delete tags
    else {
        if ([self.ranges count] != 0 && cursorLocation < currentTagsRange) {
            int rangeLength = [self.ranges count];
            int toBeRemovedIndex = 0;
            for (int i = 0; i< rangeLength; i++) {
                if (cursorLocation >= [[self.ranges objectAtIndex:i][0] intValue]
                    && cursorLocation <= [[self.ranges objectAtIndex:i][1] intValue]) {
                    toBeRemovedIndex = i;
                }
            }
            [self.tags removeObjectAtIndex:toBeRemovedIndex];
            [self updateRanges];
            NSString *outputString = @"";

            for (NSString *tag in self.tags) {
                outputString = [NSString stringWithFormat:@"%@#%@ ", outputString,
                tag];
            }
            urlField.text = outputString;
            self.identifiedTagsString = urlField.text;
            currentTagsRange = [outputString length] - 1;
        }
    }

    return YES;
}

- (void)updateRanges {
    self.ranges = [[NSMutableArray alloc] init];
    int startIndex = 0;

    for (NSString *tag in self.tags) {
        startIndex = [self.ranges count] == 0 ? 0 : [[self.ranges lastObject][1] intValue] + 1;

        int tagLength = [tag length];
        NSArray  *range = [NSArray arrayWithObjects:[NSNumber numberWithInt:startIndex], [NSNumber numberWithInt:startIndex + tagLength + 1], nil];
        [self.ranges addObject: range];
    }
}

#pragma mark UITableViewDataSource methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if (self.identifiedTagsString == NULL) {
        self.identifiedTagsString = @"";
    }
    [self.tags addObject: selectedCell.textLabel.text];

    [self updateRanges];

    NSString *output = @"";
    for (NSString *tag in self.tags) {
        output = [NSString stringWithFormat:@"%@#%@ ", output, tag];
    }

    urlField.text = output;
    User *user = [User sharedUser];
    user.autocompleteTableView.hidden = YES;

    self.identifiedTagsString = urlField.text;
    currentTagsRange = [urlField.text length];

}