Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Objective c UISearchBar搜索两个数组_Objective C_Ios_Xcode_Uisearchbar_Uisearchdisplaycontroller - Fatal编程技术网

Objective c UISearchBar搜索两个数组

Objective c UISearchBar搜索两个数组,objective-c,ios,xcode,uisearchbar,uisearchdisplaycontroller,Objective C,Ios,Xcode,Uisearchbar,Uisearchdisplaycontroller,我有一个搜索栏,搜索数组,并用结果更新UITableView。表视图是一个书籍列表,包含标题和作者: 现在,搜索栏只搜索标题,但我想让它搜索作者。这是我的搜索代码(我从中获得) } 数据源是包含标题的NSMutable数组。包含作者的数组称为“作者”。“tableData”是一个数组,用于存储应显示在屏幕上的单元格(包含正在搜索的术语的单元格) 非常感谢 Luke我会修改数据源数组,通过创建一个包含键值对的NSDictionary来包含标题和作者(Book类会更好) 之后,您可以将搜索方法改为

我有一个搜索栏,搜索数组,并用结果更新UITableView。表视图是一个书籍列表,包含标题和作者:

现在,搜索栏只搜索标题,但我想让它搜索作者。这是我的搜索代码(我从中获得)

}

数据源是包含标题的NSMutable数组。包含作者的数组称为“作者”。“tableData”是一个数组,用于存储应显示在屏幕上的单元格(包含正在搜索的术语的单元格)

非常感谢


Luke

我会修改数据源数组,通过创建一个包含键值对的NSDictionary来包含标题和作者(Book类会更好)

之后,您可以将搜索方法改为使用NSDictionary

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    [tableData removeAllObjects];

    if(searchText != nil && ![searchText isEqualToString:@""]){

        for(NSDictionary * book in dataSource){
            NSString * title = [book objectForKey:@"TITLE"];
            NSString * author = [book objectForKey:@"AUTHOR"];

            NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
            NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];

            if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
                [tableData addObject:book];
            }

    }

    [tableView reloadData];
}
注意:使用此方法,您可以更改cellForRowAtIndexPath方法以处理NSDictionary而不是标题字符串。

-(void)searchBar:(UISearchBar*)searchBar1 textDidChange:(NSString*)searchText
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
{
  temp_array1 =[array_Main1 mutableCopy];
  temp_array2 =[array_Main2 mutableCopy];
  temp_array3 =[array_Main3 mutableCopy];
}
 else
{
  [temp_array1 removeAllObjects];
  [temp_array2 removeAllObjects];
  [temp_array3 removeAllObjects];
   int g = 0;
   for (int i=0; i< array_Main1.count;i++)
    {
        NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (Range1.location != NSNotFound ||  Range2.location != NSNotFound ||  Range3.location != NSNotFound )
        {
            [temp_array1 addObject:[array_Main1 objectAtIndex:g]];
            [temp_array2 addObject:[array_Main2 objectAtIndex:g]];
            [temp_array3 addObject:[array_Main3 objectAtIndex:g]];

        }
        g++;
    }
}
[table reloadData];
}
{ 如果([searchText length]==0) { temp_array1=[array_Main1 mutableCopy]; temp_array2=[array_Main2 mutableCopy]; temp_array3=[array_Main3 mutableCopy]; } 其他的 { [临时阵列1移除所有对象]; [临时阵列2移除所有对象]; [临时阵列3移除所有对象]; int g=0; for(int i=0;i
完美答案@Joel Kravets
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    [tableData removeAllObjects];

    if(searchText != nil && ![searchText isEqualToString:@""]){

        for(NSDictionary * book in dataSource){
            NSString * title = [book objectForKey:@"TITLE"];
            NSString * author = [book objectForKey:@"AUTHOR"];

            NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
            NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];

            if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
                [tableData addObject:book];
            }

    }

    [tableView reloadData];
}
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
{
  temp_array1 =[array_Main1 mutableCopy];
  temp_array2 =[array_Main2 mutableCopy];
  temp_array3 =[array_Main3 mutableCopy];
}
 else
{
  [temp_array1 removeAllObjects];
  [temp_array2 removeAllObjects];
  [temp_array3 removeAllObjects];
   int g = 0;
   for (int i=0; i< array_Main1.count;i++)
    {
        NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (Range1.location != NSNotFound ||  Range2.location != NSNotFound ||  Range3.location != NSNotFound )
        {
            [temp_array1 addObject:[array_Main1 objectAtIndex:g]];
            [temp_array2 addObject:[array_Main2 objectAtIndex:g]];
            [temp_array3 addObject:[array_Main3 objectAtIndex:g]];

        }
        g++;
    }
}
[table reloadData];
}
- This is Helpful when you search from Dictionary.

NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;

// firstSection is array which already filled.
// contentList array for value of particular key
// filteredContentList is search array from actual array.

  - (void)searchTableList {
        NSString *searchString = searchBar.text;

        NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString];
        NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate];
        if(contentList.count > 0)
            [contentList removeAllObjects];
        [filteredContentList addObjectsFromArray:filteredArr];
    }

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1 {

        if ([searchBar1.text length] != 0)
            isSearching = YES;
        else
            isSearching = NO;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        NSLog(@"Text change - %d",isSearching);

        //Remove all objects first.
        [filteredContentList removeAllObjects];

        if([searchText length] != 0) {
            isSearching = YES;
            [self searchTableList];
        }
        else {
            isSearching = NO;
        }
        [tblFrameList_SComplete reloadData];
    }

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Cancel clicked");
    }

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Search Clicked");
        [self searchTableList];
    }