私のプロジェクトの概要: 特定のデータ (すべての静的データ) は、コア データに格納されます。アプリケーションには、検索するテキスト フィールドがあります。NSPredicate を使用して、検索文字列をコア データに存在するデータと照合しています。見つかったら、検索ボタンをクリックするとテーブル ビューに表示されます。
ブロッカー: Fetched Results Controller が何も返さないため、データが表示されない
以下は私がやっていることです。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
appDelegate = (ADAppDelegate*)[[UIApplication sharedApplication]delegate];
loginViewController = [[ADAssociateLoginViewController alloc]init];
self.searchGUIDTextField.hidden = YES;
self.searchPagecountTextField.hidden = YES;
self.searchUserIdTextField.hidden = YES;
self.searchResultsTableView.hidden = YES;
self.searchResultsArray = [[NSMutableArray alloc]init];
//self.fetchedResultsController = [[NSFetchedResultsController alloc]init];
firstName = [NSMutableArray arrayWithObjects:@"Dipanjan", @"Lokesh", @"Manish", @"Manpreet", nil];
lastName = [NSMutableArray arrayWithObjects:@"Dutta", @"Gahlawat", @"Bhardwaj", @"Kaur", nil];
globalID = [NSMutableArray arrayWithObjects:@"86077", @"86088", @"95632", @"95741", nil];
location = [NSMutableArray arrayWithObjects:@"Offshore", @"Offshore", @"Offshore", @"Offshore", nil];
subCircle = [NSMutableArray arrayWithObjects:@"TME", @"MEA", @"TME", @"AP", nil];
locationTelNo = [NSMutableArray arrayWithObjects:@"001", @"010", @"002", @"012", nil];
extension = [NSMutableArray arrayWithObjects:@"7777", @"7777", @"7887", @"9254", nil];
cityName = [NSMutableArray arrayWithObjects:@"Bangalore", @"Hyderabad", @"Bangalore", @"Chennai", nil];
countryName = [NSMutableArray arrayWithObjects:@"India", @"India", @"India", @"India", nil];
mobileNo = [NSMutableArray arrayWithObjects:@"123", @"234", @"345", @"456", nil];
rmName = [NSMutableArray arrayWithObjects:@"SP", @"KM", @"KM", @"SKG", nil];
rmEmail = [NSMutableArray arrayWithObjects:@"s_p", @"k_m", @"k_m", @"s_kg", nil];
aicName = [NSMutableArray arrayWithObjects:@"MK", @"MK", @"AB", @"BC", nil];
aicEmail = [NSMutableArray arrayWithObjects:@"m_k", @"m_k", @"a_b", @"b_c", nil];
int i;
NSManagedObject* associateSearchObject = [NSEntityDescription insertNewObjectForEntityForName:@"AssociateDetails" inManagedObjectContext:appDelegate.managedObjectContext];
for (i = 0; i < [firstName count]; i++) {
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [firstName objectAtIndex:i]] forKey:@"firstName"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [lastName objectAtIndex:i]] forKey:@"lastName"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [globalID objectAtIndex:i]] forKey:@"gid"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [location objectAtIndex:i]] forKey:@"location"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [subCircle objectAtIndex:i]] forKey:@"subCircle"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [locationTelNo objectAtIndex:i]] forKey:@"locationTelNo"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [extension objectAtIndex:i]] forKey:@"extension"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [cityName objectAtIndex:i]] forKey:@"city"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [countryName objectAtIndex:i]] forKey:@"country"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [mobileNo objectAtIndex:i]] forKey:@"mobileNo"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [rmName objectAtIndex:i]] forKey:@"rm"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [rmEmail objectAtIndex:i]] forKey:@"rmEmail"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [aicName objectAtIndex:i]] forKey:@"aic"];
[associateSearchObject setValue:[NSString stringWithFormat:@"%@", [aicEmail objectAtIndex:i]] forKey:@"aicEmail"];
NSLog(@"associateSearchObject has %@", associateSearchObject);
}
NSError* error;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"AssociateSearch" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSArray* fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject* info in fetchedObjects) {
self.searchUserIdTextField.text = [info valueForKey:@"userId"];
self.searchPagecountTextField.text = [info valueForKey:@"pageCount"];
self.searchGUIDTextField.text = [info valueForKey:@"guid"];
}
}
-(IBAction)searchAssociate:(id)sender{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AssociateDetails" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
predicate = [NSPredicate predicateWithFormat:@"firstName == %@ || lastName == %@ ||gid == %@ || location == %@ ||subCircle == %@ ||locationTelNo == %@ ||extension == %@ ||city == %@ ||country == %@ ||mobileNo == %@ ||rm == %@ ||rmEmail == %@ ||aic == %@ ||aicEmail == %@ ", [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ]];
[fetchRequest setPredicate:predicate];
NSLog(@"predicate is %@", predicate);
NSLog(@"fetchrequest is %@", fetchRequest);
// fetchrequest is { }
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
NSLog(@"count here is %d", [[aFetchedResultsController fetchedObjects]count]);
// count returned 0
self.fetchedResultsController = aFetchedResultsController;
self.fetchedResultsController.delegate = self;
NSLog(@"count is %d", [[self.fetchedResultsController fetchedObjects]count]);
// count returned 0
[self.searchResultsTableView reloadData];
self.searchResultsTableView.hidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.fetchedResultsController fetchedObjects]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString* displayString = [NSString stringWithFormat:@"%@",[self.fetchedResultsController fetchedObjects]];
cell.textLabel.text = [NSString stringWithFormat:@"%@", displayString];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
detailsViewController = [[ADAssociateDetailsViewController alloc]initWithNibName:@"ADAssociateDetailsViewController" bundle:nil];
[self.navigationController pushViewController:detailsViewController animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
私の問題を解決するのを手伝ってください。NSPredicate に取り組むのはこれが初めてです。