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

时间:2014-09-22 11:26:11

标签: 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"];

在表格行中只显示了名称

当我不搜索任何内容时,当用户通过编写

按任意行时,我可以获得整个对象
[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;

}

1 个答案:

答案 0 :(得分:1)

希望这会对你有所帮助

据我所知,您只想要在搜索栏中包含哪些字符集,

  1. 首先使用NSMutableArray *nameJson从json添加你的所有名字。 并使用NSArray *nameShowing

    _nameShowing = _nameJson;

    并重新加载tableview

  2. 现在你已经在表格视图中显示了你的所有名字,现在你搜索了特定的名字

    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];
        }