【问题标题】:Choose the table view when I should search words当我应该搜索单词时选择表格视图
【发布时间】:2015-04-16 06:21:15
【问题描述】:

我在一个视图控制器和一个搜索栏中有 2 个表。第一个视图控制器和搜索栏的功能还可以,直到我添加了第二个表。现在,当我想搜索某些东西时,研究在第一张表中,我不想要这个。

这是我的代码:

- (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。

【问题讨论】:

    标签: ios objective-c uitableview uisearchbar


    【解决方案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。我查看了 Apple 的文档和一些示例,似乎 searchDisplayController 带有自己创建的 tableview,因此您不必创建另一个 tableview。你可以区分这两个表,看看我的编辑
    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    相关资源
    最近更新 更多