关于iOS开发的自动完成功能的任何想法?

时间:2013-02-09 05:11:58

标签: ios xcode uitextfield autosuggest

任何人都可以向我指出任何关于如何创建自动填充文本字段的示例项目或提示,其中用户键入一个通用名称,例如“美国航空公司”,其具有另一个值,例如“AA”(对于美国航空公司)和用户类型根据文本字段中的内容建议结果?

像这样:example

任何提示都会很棒!

谢谢大家。

3 个答案:

答案 0 :(得分:2)

First get this source code

根据我的经验在IB中添加搜索显示控制器。然后我将代码添加到RootViewController.m中,如下所示:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
 NSInteger searchOption = controller.searchBar.selectedScopeButtonIndex;
 return [self searchDisplayController:controller shouldReloadTableForSearchString:searchString searchScope:searchOption];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
 NSString* searchString = controller.searchBar.text;
 return [self searchDisplayController:controller shouldReloadTableForSearchString:searchString searchScope:searchOption];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString*)searchString searchScope:(NSInteger)searchOption {

 NSPredicate *predicate = nil;
 if ([searchString length])
  if (searchOption == 0) // full text, in my implementation.  Other scope button titles are "Author", "Title"
   predicate = [NSPredicate predicateWithFormat:@"title contains[cd] %@ OR author contains[cd] %@", searchString, searchString];
  else
   // docs say keys are case insensitive, but apparently not so.
   predicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", [[controller.searchBar.scopeButtonTitles objectAtIndex:searchOption] lowercaseString], searchString];
 [fetchedResultsController.fetchRequest setPredicate:predicate];

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  abort();
    }           

 return YES;
}

This is tutorial for help you.

And also get demo project.

<强>感谢。 :)

答案 1 :(得分:1)

看看我的实施,MLPAutoCompleteTextField。它是开源的。

它可以自动完成,类似于谷歌的即时搜索。它使用Levenshtein Distance算法对条目进行排序。

答案 2 :(得分:0)

参见我的演示项目:

http://olegh.cc.st/autocomplete.html

尝试在“城市”字段中搜索“约克”,或在“域名”字段中搜索“预订”

这是你在寻找什么?