Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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,我在一个视图控制器和一个搜索栏中有两个表。第一个视图控制器和搜索栏的功能正常,直到我添加了第二个表。现在,当我想搜索一些东西时,研究结果在表1中,我不想要这个 这是我的密码: - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { [self.searchResult removeAllObjects]; NSPredicate *resultPredicate = [NS

我在一个视图控制器和一个搜索栏中有两个表。第一个视图控制器和搜索栏的功能正常,直到我添加了第二个表。现在,当我想搜索一些东西时,研究结果在表1中,我不想要这个

这是我的密码:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.searchResult removeAllObjects];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];

    self.searchResult = [NSMutableArray arrayWithArray: [self.tableData filteredArrayUsingPredicate:resultPredicate]];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    return YES;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==1) {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResult count];
    }
    else
    {return [self.tableData count];}

    }
    else return historique.count;
}


- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
    return [[content objectAtIndex:section] objectForKey:@"headerTitle"];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [content valueForKey:@"headerTitle"];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [indices indexOfObject:title];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (tableView.tag==1) {
        return  [content count];
    }else
    return 1;
}


UITableViewCell *cell ;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    if (tableView.tag==0) {

 cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        if (tableView == self.searchDisplayController.searchResultsTableView)
        {
            cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

        }
        else

        cell.textLabel.text = [historique objectAtIndex:indexPath.row];

    }


    if (tableView.tag==1) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else
    {
        cell.textLabel.text = self.tableData[indexPath.row];
    }}
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DefinitionViewController *secondViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"definition"];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.textLabel.text;
    NSString *uu;

    if ([[transaction valueForKey:str] isKindOfClass:[NSArray class]]) {

        NSLog(@"yes");
        NSArray *jsonArray = (NSArray *)[transaction valueForKey:str];
        uu = [jsonArray componentsJoinedByString:@"\n"];
        secondViewController.definitionsArray=jsonArray;


    }

    else{
        uu=[transaction valueForKey:str];

    }

    NSMutableArray *values = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"histroriqueValues"]];
    [values addObject:uu];
    [[NSUserDefaults standardUserDefaults] setObject:values forKey:@"histroriqueValues"];
    historiqueValue = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"histroriqueValues"]mutableCopy];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

tableView.tag=0是我要进行搜索的表。我放置断点,当我单击搜索栏时,搜索所在的表是tableView.tag=1。

我用UITableViewController创建了一个小示例项目。如果你需要,我可以把它推到github回购协议上,这样你就可以抓住它并在它的基础上继续发展

//
//  ViewController.m
//  filterExample
//
//  Created by François Chabbey on 16.04.15.
//  Copyright (c) 2015 François Chabbey. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSArray *content;
    NSArray *filteredContent;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    content = @[@"CECI",@"CELA",@"CELUI-CI",@"ET CA",@"ETC"];
    self.tableView.dataSource = self;
    self.searchDisplayController.delegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    self.searchDisplayController.searchResultsDelegate = self;
    self.searchDisplayController.searchBar.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {

}


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

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
    filteredContent = [NSArray arrayWithArray:[content filteredArrayUsingPredicate:predicate]];
    return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    return YES;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView  == self.tableView) {
        return content.count;
    } else {
        return filteredContent.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"];
    if (tableView == self.tableView) {
        cell.textLabel.text =  content[indexPath.row];
    } else {
        cell.textLabel.text = filteredContent[indexPath.row];
    }
    return cell;
}


@end

即使我把self.tableSource=self.searchDisplayController.searchResultsTableView;结果是一样的。您是否在代码中实例化了这两个tableview。我看了一下苹果的文档和一些示例,似乎searchDisplayController自带了自己创建的tableview,所以您不必再创建另一个tableview。你可以区分这两张表,看看我的编辑