ios - Objective-C 主题标签/标签自动完成实现

标签 ios objective-c tags hashtag

我正在研究使用 Objective-C 实现主题标签自动完成,如图所示

enter image description here

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

最佳答案

我最终写了一些我觉得有点难看但有效的函数。也许有一些更有效的方法来实现它。

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

}

关于ios - Objective-C 主题标签/标签自动完成实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22886688/

相关文章:

ios - 当子类化 UIView 并将其添加到界面构建器中时,不会调用 drawRect

objective-c - NSURL - 没有可变子类,所以不需要 "copy"作为属性?

ios - UITableViewController 手动完成

objective-c - 为什么我可以在CGSizeMake和CGRectMake中使用整数?

html - 如何在 Sublime Text 3 中启用智能 HTML 标签缩进?

java - 如何在两个xml标签之间输出?

ios - 如何在 xcode 8 中创建没有开发者帐户的 ipa 文件

ios - 在单元格中添加和修改多个约束?

ios - 在 iPhone 之间传输数据的最佳选择

c++ - 空结构或匿名结构作为标签