Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone IOS:使用UISearchbar自动完成搜索_Iphone_Ios_Autocomplete_Uisearchbar_Uisearchdisplaycontroller - Fatal编程技术网

Iphone IOS:使用UISearchbar自动完成搜索

Iphone IOS:使用UISearchbar自动完成搜索,iphone,ios,autocomplete,uisearchbar,uisearchdisplaycontroller,Iphone,Ios,Autocomplete,Uisearchbar,Uisearchdisplaycontroller,我有一个由文本字段组成的NSMutableArray。我想在用户在UISearchBar中键入时加载它们。最初,我不想在用户开始键入之前加载所有文本字段。只有用户开始输入需要加载的第一个字母建议。有什么帮助吗?有很多逻辑,但我把我的逻辑放在这里: 在ViewDidLoad方法中,使用两个NSMutableArray和将一个数组添加到另一个数组中 self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1 self.It

我有一个由文本字段组成的
NSMutableArray
。我想在用户在
UISearchBar
中键入时加载它们。最初,我不想在用户开始键入之前加载所有文本字段。只有用户开始输入需要加载的第一个字母建议。有什么帮助吗?

有很多逻辑,但我把我的逻辑放在这里:

ViewDidLoad
方法中,使用两个
NSMutableArray
将一个数组添加到另一个数组中

self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:@"YorArrayList", nil]; // array no - 2 

[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array
并编写以下UISearchBar的委托方法

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
        NSString *name = @"";
        NSString *firstLetter = @"";

    if (self.listOfTemArray.count > 0)
         [self.listOfTemArray removeAllObjects];

        if ([searchText length] > 0)
        {
                for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
                {
                        name = [self.ItemOfMainArray objectAtIndex:i];

                        if (name.length >= searchText.length)
                        {
                                firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
                                //NSLog(@"%@",firstLetter);

                                if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
                                {
                                    // strings are equal except for possibly case
                                    [self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
                                    NSLog(@"=========> %@",self.listOfTemArray);
                                }
                         }
                 }
         }
         else
         {
             [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
         }

        [self.tblView reloadData];
}
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本
{
NSString*name=@;
NSString*firstLetter=@;
如果(self.listofTermarray.count>0)
[self.listofteray removeAllObjects];
如果([searchText length]>0)
{
for(int i=0;i<[self.itemof主数组计数];i=i+1)
{
name=[self.ItemOfMainArray对象索引:i];
如果(name.length>=searchText.length)
{
firstLetter=[name substringWithRange:NSMakeRange(0[searchText-length]);
//NSLog(@“%@”,第一个字母);
if([firstLetter caseInsensitiveCompare:searchText]==SensorDeredName)
{
//除了可能的大小写外,字符串是相等的
[self.listofTermarray addObject:[self.ItemOfMainArray objectAtIndex:i]];
NSLog(@“=============>%@”,self.ListToFray);
}
}
}
}
其他的
{
[self.listofTermarray addObjectsFromArray:self.ItemOfMainArray];
}
[self.tblView reloadData];
}
}
控制台中显示输出。


此代码可能会对您有所帮助。

我认为最好的方法是构建您自己的NSCompoundPredicate:

  NSString *nameVar = ...; //ex: smith m
    NSArray *names = ...; //ex: John Smith, Mary Smith

    NSArray *terms = [nameVar componentsSeperatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSMutableArray *subpredicates = [NSMutableArray array];

    for(NSString *term in terms) {
      if([term length] == 0) { continue; }
      NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@", term];
      [subpredicates addObject:p];
    }

    NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
    [fetchController setPredicate:filter];
享受编码