【问题标题】:UISearchBar shows "No Results" whether if there are search text matches or not无论是否有搜索文本匹配,UISearchBar 都显示“无结果”
【发布时间】:2014-03-16 16:46:13
【问题描述】:

我正在使用表格视图搜索栏来搜索我的核心数据内容并在UITableView 中显示匹配的内容。

问题是,如果您输入与某些内容匹配的搜索文本,TableView 是否显示“无结果”。

谁能告诉我为什么?

TableViewController 代码:

 (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
        [context deleteObject:[self.cart objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

        // Remove device from table view
        [self.cart removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//Searchbar
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredProductsArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Cart.cartProductName contains[c] %@",searchText];
    filteredProductsArray = [NSMutableArray arrayWithArray:[cartProducts filteredArrayUsingPredicate:predicate]];
}

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

核心数据内容在 Cart.h 中,属性为 cartProductName,即 NSString。

更新:

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

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

    if (cell.accessoryView == nil) {
        // Only configure the Checkbox control once.
        cell.accessoryView = [[Checkbox alloc] initWithFrame:CGRectMake(0, 0, 25, 43)];
        cell.accessoryView.opaque = NO;
        cell.backgroundColor = [UIColor clearColor];

       [(Checkbox*)cell.accessoryView addTarget:self action:@selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"cartProductName"]]];
     //[(Checkbox*)cell.accessoryView setChecked: [((Cart*)device).checked boolValue]];

    //Accessibility

    [self updateAccessibilityForCell:cell];

return cell;
}
    return nil;
}

更新:CartTableViewController.m:

#import "CartViewController.h"
#import "Checkbox.h"
#import "Cart.h"


@interface CartViewController ()
@property (strong) NSMutableArray *cart;

@end

@implementation CartViewController {
    NSArray *cartProducts;
}

@synthesize filteredProductsArray;
@synthesize searchBar;

【问题讨论】:

    标签: ios uitableview core-data uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    您提供了一些细节来了解问题所在,但我猜您的 -(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 应该是这样的:

    [self.filteredProductsArray removeAllObjects];
    
    NSFetchRequest *fetchRequest = // set up the fetch request for your Cart entity here...
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cartProductName contains[cd] %@", searchText];
    fetchRequest.predicate = predicate;
    
    NSError *error = nil;
    NSArray *results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (results == nil) {
        // handle error here...
    }
    
    [self.filteredProductsArray addObjectsFromArray:results];
    

    可以在Searching in UITableView 找到一个关于如何实现此目标的非常好的示例。

    性能考虑。当使用cd 时,研究会比不使用它慢。所以,你可以直接使用contains。

    希望对您有所帮助。

    【讨论】:

    • 您好,感谢您迄今为止的帮助。你需要什么样的细节?遗憾的是,您的解决方案产生了一些错误:1.'使用未声明的标识符'谓词',2.'意外的接口名称'NSPredicate':预期的表达式'
    • 你听从我的建议了吗? @user255368
    • GitHub 上的教程?怎么下载?
    • 我提供的 sn-p。未创建提取请求。此外,如果您点击我提供的链接,您可以查看源代码。 @user255368
    • 您是否正确实现了表视图委托方法? @user255368
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多