ios - 搜索栏xcode

标签 ios json xcode5 searchbar

我有一个用于IOS的新应用,其中正在解析JSON并且正在使用搜索栏。解析JSON可以正常工作,但是搜索栏存在问题,即在构建应用程序时,我想搜索一些已解析的单词时,什么也看不到。下面,我把我的代码部分。我认为谓词存在问题:“predicateWithFormat:@” productName like [cd]%@“,searchText];”谁能帮我?

tableViewController.m

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productName like[cd] %@", searchText];

    searchResults = [productsArray filteredArrayUsingPredicate:predicate];
}



-(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 == self.searchDisplayController.searchResultsTableView){
       return [searchResults count];  
    } else {
        return [productsArray count]; }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    Product *productObject;
    productObject = [productsArray objectAtIndex:indexPath.row];

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

    if (tableView == self.searchDisplayController.searchResultsTableView) 
    {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = productObject.productName;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"Cell" sender: self];
    }
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"Cell"]) {

        DetailViewController *destViewController = segue.destinationViewController;



        if ([self.searchDisplayController isActive]) {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            destViewController.imageView = [searchResults objectAtIndex:indexPath.row];

        } else {

            //indexPath = [self.tableView indexPathForSelectedRow];

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            Product * object =[productsArray objectAtIndex:indexPath.row];
            [[segue destinationViewController] getProduct: object];
            //destViewController.idLabel = [productsArray objectAtIndex:indexPath.row];
        }
    }
}

最佳答案

如您所怀疑,此问题最有可能与谓词设置有关。

我正在使用类似的方法在联系人列表中搜索匹配项,也许这是一个可行的选择:

[NSPredicate predicateWithBlock:^BOOL(Contact *contact, NSDictionary *bindings){
    return [contact.name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound;
}];

关于ios - 搜索栏xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186157/

相关文章:

objective-c - UIView中的UIImage和显示图片部分越界

javascript - 为什么在 Angular 中使用时 json 数组值会转换为字符串

java - 如何使用 Java 中的 GSON API 自定义 JSON 中的特定日期字段?

ios - 如何在 swift 4 中使用 json 对象发送 json 数据

ios - -[WelcomeUI _setViewDelegate :]: unrecognized selector sent to instance 0xc061110

core-data - 核心数据改变本地存储观察者/通知

ios - 在 Swiftui 中如何通过旋转到横向自动关闭模态

iOS 13.0 - 支持深色模式并支持 iOS 11 和 12 的最佳方法

ios - 处理 Xcode 5 上的文本对齐折旧

iphone - 我的代码片段泄露了