我在Xcode项目中实现了一个搜索栏来过滤核心数据记录。问题是Seach接缝工作(搜索数组正确填充,tableview的行数正确显示)但Cell是空的。 这是我的代码:
h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface iDataTable : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> {
int nonume;
NSString *nume;
NSArray *arrTitle;
}
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSString *nume;
@property (nonatomic) int nonume;
@property (nonatomic, retain) NSMutableArray *arrSearchRes;
@end
m file:
#import "iDataTable.h"
#import "AppDelegate.h"
@interface iDataTable ()
@end
@implementation iDataTable
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize nume, nonume;
@synthesize arrSearchRes;
- (void)viewDidLoad{
[super viewDidLoad];
self.arrSearchRes = [NSMutableArray arrayWithCapacity:[[self.fetchedResultsController fetchedObjects] count]];
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:nume ///// language
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(nume != '')"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"data" ascending:NO]];
[request setPredicate:pred];
NSError *error;
arrTitle = [context executeFetchRequest:request
error:&error];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.arrSearchRes count];
}
else {
return [arrTitle count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell != nil)
{
NSManagedObject* matches = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
matches = [arrSearchRes objectAtIndex:indexPath.row]; // get search data
}
else {
matches = [arrTitle objectAtIndex:indexPath.row]; // get general data
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[matches valueForKey:@"nume"]];
}
else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:nume ///// language
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(nume != %@)", searchText];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"data" ascending:NO]];
[self.arrSearchRes removeAllObjects];
[request setPredicate:pred];
NSError *error;
NSArray *arr = [context executeFetchRequest:request error:&error];
self.arrSearchRes = [[NSMutableArray alloc] initWithArray:arr];
NSLog(@"%@, %@", arrSearchRes, searchText); // ok - arrSearchRes looks ok
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
谢谢!
答案 0 :(得分:0)
这对我有用:
[self.searchDisplayController.searchResultsTableView reloadData];