简体   繁体   English

使用UISearchDisplayController时显示搜索结果崩溃

[英]Displaying search results crashes when using UISearchDisplayController

I tried to implement a SearchBarView Controller to my TableView with CoreData. 我尝试使用CoreData对我的TableView实现SearchBarView控制器。 But, when I tap on the Searchbar, the app crashes and I get the following Error: 但是,当我点击搜索栏时,应用程序崩溃,并且出现以下错误:

Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:5439 / [UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:],/ SourceCache / UIKit / UIKit-2935.137 / UITableView.m:5439中的断言失败
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为Cell的单元出队-必须为该标识符注册一个笔尖或一个类,或在情节提要中连接原型单元”

Without Searchbar I show my TableContent like this: 没有搜索栏,我这样显示我的TableContent:

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

    // Configure the cell...
    NSManagedObject *device = [self.inventory objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];
    //[cell.detailTextLabel setText:[device valueForKey:@"company"]];

    return cell;
}

works perfectly. 完美地工作。

So now I tried to show the SearchbarViewControllerContent like this: 所以现在我试图显示SearchbarViewControllerContent像这样:

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

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [searchResults objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];

    return cell;
}

but this doesn't work. 但这不起作用。

If you have to know how my Searchbar (should) works: 如果您必须了解我的搜索栏的工作方式(应该):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"productName contains[c] %@", searchText];
    searchResults = [cartProducts filteredArrayUsingPredicate:resultPredicate];
}

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

    return YES;
}

您必须在viewDidLoad方法中注册搜索tableView的单元格,例如:

  [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:<Nib Name> bundle:nil] forCellReuseIdentifier:<cellIdentifier>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM