【问题标题】:How to add a search bar to a TableView that uses an array with NSDictionaries如何将搜索栏添加到使用带有 NSDictionaries 的数组的 TableView
【发布时间】:2013-05-24 14:09:02
【问题描述】:

到目前为止,我为使搜索栏正常工作所做的工作...

- (void)viewDidLoad
{

[self makeData];

[super viewDidLoad];

self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;

//Feedback button code here
UIBarButtonItem *feedback;
feedback = [[UIBarButtonItem alloc] initWithTitle:@"Contact"   style:UIBarButtonItemStylePlain
target:self action:@selector(showEmail:)];
self.navigationItem.leftBarButtonItem = feedback;


[self.tableView reloadData];
self.tableView.scrollEnabled = YES;

}

-(void) makeData; {

list = [[NSMutableArray alloc] init];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Acceleration", @"name" ,
                @"acceleration.png", @"image" ,
                @"Acceleration", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Alternating Current", @"name" ,
                @"alternating current.png", @"image" ,
                @"Alternating Current", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Ampere", @"name" ,
                @"ampere.png", @"image" ,
                @"Ampere", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Amplitude", @"name" ,
                @"amplitude.png", @"image" ,
                @"Amplitude", @"description" ,nil]];


}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    filteredList = [[NSMutableArray alloc] init];

    for (NSString *str in list) {
        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredList addObject:str];
        }
    }
}
[self.myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView reloadData];
}

然而,每次我尝试在应用程序中输入内容时都会崩溃并收到以下消息:

2013-05-29 11:36:09.734 PhysEX[4770:c07] -[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0
2013-05-29 11:36:09.735 PhysEX[4770:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0'

如何让我的搜索工作?

【问题讨论】:

    标签: uitableview nsarray nsdictionary uisearchbar nsmutabledictionary


    【解决方案1】:

    textDidChange 委托中的以下代码不正确:

    for (NSString *str in list) {
        ...
    }
    

    这是因为列表数组中的对象不是 NSString 而是 NSDictionary。因此,错误消息指出 NSDictionary 无法识别选择器 rangeOfString:options: 因为该选择器用于 NSString 实例。

    您需要改为搜索 NSDictionary 值。

    for (NSDictionary *theDictionary in list) {
    
        for (NSString *key in theDictionary) {
            NSString *value = [theDictionary objectForKey:key];
            NSRange stringRange = [value rangeOfString:searchText options:NSCaseInsensitiveSearch];
    
            if (stringRange.location != NSNotFound) {
                [filteredList addObject:theDictionary];
                break;
            }
       }
    }
    

    【讨论】:

    • 完美,非常感谢!现在我的搜索工作了,但是每当我点击搜索结果中的结果时,它都会链接到与原始表格视图中相同定位单元格对应的图像,所以如果加速度是原始的顶部并且我搜索了安培,当我点击时安培在搜索结果中需要我加速吗?
    • @WilliamRose 如果您当前处于搜索模式,则需要修改didSelectRowAtIndexPath 中的代码以使用filteredList。
    • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *definitionDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; definitionDetail.definitionImageString = [[NSString alloc] initWithString:[[list objectAtIndex:indexPath.row] objectForKey:@"image"]]; definitionDetail.title = [[list objectAtIndex:indexPath.row] objectForKey:@"name"]; [self.navigationController pushViewController:definitionDetail Animation:YES]; }
    • - (void)tableView:(UISearchDisplayController *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *definitionDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; definitionDetail.definitionImageString = [[NSString alloc] initWithString:[[filteredList objectAtIndex:indexPath.row] objectForKey:@"image"]]; definitionDetail.title = [[filteredList objectAtIndex:indexPath.row] objectForKey:@"name"]; [self.navigationController pushViewController:definitionDetail Animation:YES]; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多