【问题标题】:Objective-C hashtag/tags autocomplete implementationObjective-C 主题标签/标签自动完成实现
【发布时间】:2014-05-18 04:15:17
【问题描述】:

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

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

【问题讨论】:

  • 自动完成是什么意思?我很确定自动完成主题标签与自动完成普通文本完全一样。
  • @JoshCaswell 感谢您的回复!我确实查找了自动完成功能,但我正在寻找更具体的添加和删除主题标签的实现。例如,主题标签应立即整体删除。

标签: ios objective-c tags hashtag


【解决方案1】:

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

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

}

【讨论】:

  • tags、currentTagsRange、identifiedTagsStringLength 等不在sn-p中。还没满。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 2015-02-18
  • 2016-02-17
  • 2019-03-31
  • 2011-06-01
  • 2023-03-25
相关资源
最近更新 更多