Вопрос

I was looking into implementing hashtag autocomplete with objective-C as shown in the picture

enter image description here

I found it a bit difficult than expected. I'm looking more specific implementation for adding and deleting hashtags. For example, the hashtag should be deleted as a whole at once. I was wondering if anyone has similar experience implemented it and if there's a more efficient way implemented it. Thanks

Это было полезно?

Решение

I ended up writing some functions that I feel is a bit ugly but it works. Maybe there are some more efficient ways to implement it.

- (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];

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top