Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Ios 使用搜索控制器时如何获取正确的对象_Ios_Objective C_Uitableview_Uisearchbar - Fatal编程技术网

Ios 使用搜索控制器时如何获取正确的对象

Ios 使用搜索控制器时如何获取正确的对象,ios,objective-c,uitableview,uisearchbar,Ios,Objective C,Uitableview,Uisearchbar,我正在使用searchdisplaycontroller,我有一个上面的表视图,我有一个搜索栏。。。表视图显示对象名称的列表。。例如,如果我有一个json对象,比如 json :{ name: testname; age:12; maths:50; english:56; science:45; }, { // list of similar json objects } 我使用cell.name.text=[json valueforKey:@“name”]在表行中只显示了名称 当我不搜索任

我正在使用searchdisplaycontroller,我有一个上面的表视图,我有一个搜索栏。。。表视图显示对象名称的列表。。例如,如果我有一个json对象,比如

json :{
name: testname;
age:12;
maths:50;
english:56;
science:45;
},
{
// list of similar json objects
}
我使用
cell.name.text=[json valueforKey:@“name”]在表行中只显示了名称

当我不搜索任何东西时,当任何用户通过书写按任意行时,我都可以得到整个对象

[json objectAtIndex:indexPath.row];
我的问题是当我搜索表时,我没有在索引处得到正确的对象。。。例如,我在搜索栏中搜索某些内容,它会显示包含相关对象的行。当我使用相同的
[json objectAtIndex:indexPath.row]分配了错误的索引对象请帮助这里是我的代码

-(void)filtercontentForSearchText:(NSString *)searchtext scope:(NSString *)scope{



NSPredicate *resultpredicate=[NSPredicate predicateWithFormat:@"SELF contains [cd] %@",searchtext];
    searchList=[[searchDataArray valueForKey:@"SubjectDescription" ] filteredArrayUsingPredicate:resultpredicate];




    NSLog(@"searchlist %@",searchList );
}



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


    [self filtercontentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles ] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    return YES;

}
//这是didselectrowAtindex代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected");
  //  if ([numberOfTouchesString isEqualToString:@"One"])
 //   {


       NSArray *selected = [searchDataArray objectAtIndex:indexPath.row];
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectedSubject:)])


        {
            if (tableView == self.searchDisplayController.searchResultsTableView){


                if ([self.searchDisplayController isActive]){

                    NSLog(@"searchlist %@",searchList);
                 indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
                    id searchResult = [searchList objectAtIndex:indexPath.row];
                    int indexForResult = [json indexOfObject:searchResult];
                    NSLog(@"indexpath%d " , indexForResult);

                     searchAppDelObj.didselectjsondata=[json objectAtIndex:indexPath.row];//storing that json data in AppDelegate object



                    _serachStr = [searchList objectAtIndex:indexPath.row];



                    //searchAppDelObj.valuePass=(NSArray *)_serachStr;
                      NSLog(@"   searchAppDelObj.valuePass %@",(NSArray *)_serachStr);
                      //[self.delegate selectedSubject:self];

                }

            }
            else
            {
                LearningSearchCell *cell = (LearningSearchCell *)[self.learningSearchTableView cellForRowAtIndexPath:indexPath];

                NSString *cellText = cell.subjectNameLabel.text;

                searchAppDelObj.didselectstring=cellText;

                searchAppDelObj.didSelectArray=[searchDataArray objectAtIndex:indexPath.row];

                NSLog(@"appobjarray %@",searchAppDelObj.didSelectArray);

                NSLog(@"AppObj.didselectstring %@",searchAppDelObj.didselectstring);
                //searchAppDelObj.valuePass=selected;
                //[self.delegate selectedSubject:self];
                            }

            if (-[_comparestr isEqualToString:@"search"]) {
                searchAppDelObj.valuePass=(NSArray *)_serachStr;
                NSLog(@"   searchAppDelObj.valuePass %@",(NSArray *)_serachStr);
                [self.delegate selectedSubject:self];
            }else{
                NSLog(@"AppObj.didselectstring %@",searchAppDelObj.didselectstring);
                searchAppDelObj.valuePass=selected;
                [self.delegate selectedSubject:self];
            }
            [self.revealViewController revealToggleAnimated:YES];



        }

    }
//这是CellForRowAtindexpath代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"learningSearchCellIdentifier";
    LearningSearchCell *cell = [self.learningSearchTableView dequeueReusableCellWithIdentifier:cellIdentifier ];

    if ((tableView==self.learningSearchTableView)==YES)
    {


        cell.subjectNameLabel.text=[[json objectAtIndex:indexPath.row]valueForKey:@"name"];
        //subjectString=cell.subjectLabel.text;


        cell.teacherNameLabel.text=[[json objectAtIndex:indexPath.row]valueForKey:@"age"];


        cell.subjecDataAndSessionLabel.text=[[json objectAtIndex:indexPath.row]valueForKey:@"maths"];

        NSNumber *test  =[[searchDataArray objectAtIndex:indexPath.row]valueForKey:@"Cohort"];
        NSString *myString =[NSString stringWithFormat:@"%@",test];

        cell.chorotLabel.text=myString;




    }

    else if (tableView==self.searchDisplayController.searchResultsTableView) {

        cell.subjectNameLabel.text=[searchList objectAtIndex:indexPath.row];
        NSLog(@"indexpath %d",indexPath.row);

        cell.teacherNameLabel.text=[[json objectAtIndex:indexPath.row]valueForKey:@"age"];


         cell.subjecDataAndSessionLabel.text=[[json objectAtIndex:indexPath.row]valueForKey:@"maths"];

        NSNumber *test  =[[searchDataArray objectAtIndex:indexPath.row]valueForKey:@"english"];
        NSString *myString =[NSString stringWithFormat:@"%@",test];

        cell.chorotLabel.text=myString;



    }

    else{

        cell.subjectNameLabel.text=[json objectAtIndex:indexPath.row];

    }
    return cell;

}

希望这对你有帮助

据我所知,您只需要搜索栏中包含的一组字符的名称

  • 首先使用
    NSMutableArray*nameJson
    添加所有json中的名称。 并使用
    NSArray*名称显示

    \u nameShowing=\u namesjson

    然后重新加载tableview

  • uptill现在您在表视图中有了您所有的名字,现在您搜索特定的名字

    2.    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
        {
            self.nameShowing = nil;
            self.nameShowing = [[NSArray alloc]init]; 
             NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF contains %@", self.searchbar.text];
    
            self.nameShowing  = [self.nameJson filteredArrayUsingPredicate:filter];
            if(([self.searchbar.text  isEqual:@""]))
            {
                self.nameShowing = self.nameJson;
            }
    
    
            [self.tblView reloadData];
        }
    

    发送详细的代码,以便我可以帮助您。我已编辑了此问题,请参阅@RameshMuthe