我正在使用表格视图搜索栏来搜索我的核心数据内容,并在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;
答案 0 :(得分:0)
您提供的细节很少,无法了解问题所在,但我猜您的-(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
。
希望有所帮助。