我正在尝试制作与Twitter在编写推文时的工作方式相同的方法。
我的应用程序使用与@username相同的语法来进行消息传递。在我的撰写视图中,我有一个用户输入消息的UITextView / UITextField,我想检测用户何时键入@,然后将@后面的字符串与我自己完成的用户名数组进行比较
我一直在关注这个优秀的教程: http://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values
这两种方法是我应该执行检测的地方:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
// Put anything that starts with this substring into the autocompleteUrls array
// The items in this array is what will show up in the table view
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
我认为在这里应该发现@的检测,但我不知道该怎么做,然后只比较紧跟在符号后面的字符(并且当它碰到空白时停止比较)。
标准的iOS twitter应用程序是我正在寻找的主要示例。任何帮助都会很棒,谢谢!
答案 0 :(得分:0)
使用NSScanner
。在searchAutocompleteEntriesWithSubstring
方法中,使用substring
创建一个扫描程序并在循环中运行,最初搜索@
,然后在找到它后搜索空格。然后,您可以使用扫描的字符串进行自动完成。
根据您在找到匹配项时对字符串执行的操作,指示搜索算法的工作方式。您可能希望找到@
中最后substring
的范围并将扫描仪位置设置为那里(以避免处理任何以前的用户名) - 但是,您没有关于刚更改的内容的信息所以用户返回并插入了用户名...