本文介绍了在我的TableView内部实现一个SearchBar,并使用NSFetchedResultsController和适当的谓词来获得搜索工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TableViewController有一个添加按钮,转到一个ModalViewController;用户将文本添加到文本字段中,按保存并退出,该条目将添加到Core Data中的实体和属性中,并显示在TableViewController中。它工作得很好。

I have a simple TableViewController which has an add button which goes over to a ModalViewController; the user adds in text into a text field, presses save and that dismisses and the entry is added to the Entities and Attributes in Core Data and displayed in the TableViewController. It works well.

我首先开始使用另一个按钮,在表视图中拉起一个UISearchBar,我可以搜索结果并将其拉回来;工作。然后我添加了一个新的选项卡到TableViewController的底部,并设法获取搜索栏和表。

I firstly started off with another button that pulled up a UISearchBar in a Table View and I could search for results and pull that back; that worked. I then added a new tab to the bottom of the TableViewController and managed to get the search bar and table in there.

现在,我想在单表视图的顶部的SearchBar,我希望用户能够键入,并动态搜索TableView。

Now, I want the SearchBar at the top of the Single Table View and I want the user to be able to type and for it to dynamically search the TableView.

我的fetchRequest目前对于TableView而言(没有搜索栏):

My fetchRequest looks like this currently for the TableView (without the searching bar):

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"occasion.dateOfEvent" ascending:NO selector:@selector(localizedCaseInsensitiveCompare:)];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:20];
    //NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY whoBy.name CONTAINS[c] %@", self.searchBar.text];
    //[fetchRequest setPredicate:pred];
    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"occasion.dateOfEvent" cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;
    return _fetchedResultsController;

我正在搜索整个事务等。现在,这个代码和我的其他具有搜索栏的视图控制器是NSPredicate未注释的事实。搜索工作良好。如果我在此代码中取消注释,当我运行它,我得到以下错误:

I'm searching across the Transaction, etc. Now, the only difference between this code and my other view controller which has the search bar is the fact that the NSPredicate is uncommented. The searching works well. If I uncomment that in this code, when I run it, I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid predicate: nil RHS'
*** First throw call stack:

我明白发生了什么,因为我想让TableViewController显示没有一个谓词,但是当我搜索,我去了该谓词生效。

I understand what's happening because I want the TableViewController to display WITHOUT a predicate, but when I search, I went that predicate to go in effect. Any help on how to go about getting this working would be massively appreciated!

我的搜索栏委托方法如下所示:

My search bar delegate method looks like this:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Error in search %@, %@", error, [error userInfo]);
    } else
    {
        [self.tView reloadData];
        [self.searchBar resignFirstResponder];
        [noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
    }
}


推荐答案

应该仅在给定搜索字符串时设置谓词:

You should set a predicate only if a search string is given:

if ([self.searchBar.text length] > 0) {
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY whoBy.name CONTAINS[c] %@", self.searchBar.text];
    [fetchRequest setPredicate:pred];
}

如果搜索字符串为空,则不设置谓词,

If the search string is empty, no predicate is set and therefore all results are returned.

这篇关于在我的TableView内部实现一个SearchBar,并使用NSFetchedResultsController和适当的谓词来获得搜索工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:53