嘿伙计们,我有一个UISegmented控制器,我正试图在核心数据的提取结果之间切换。但是,当分段控制器更改时,它不会更新UITableView中的获取结果。我不确定我做错了什么。希望有人可以指出我正确的方向。这是我的代码:
- (void)viewDidLoad {
NameAppDelegate *theDelegate = (NameAppDelegate*)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = theDelegate.managedObjectContext;
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[super viewDidLoad];
}
- (IBAction)segmentedControlValueChanged {
selectedUnit = tableSeg.selectedSegmentIndex;
[self.theTableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (selectedUnit == 0) {
return [[fetchedResultsController sections] count];
}
if (selectedUnit == 1) {
return [[fetchedResultsController2 sections] count];
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (selectedUnit == 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
if (selectedUnit == 1) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController2 sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (selectedUnit == 0) {
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
static NSString *cellID = @"nameCell";
NamesViewCustomCell *cell = (NamesViewCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSLog(@"New Cell Made");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NamesViewCustomCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[NamesViewCustomCell class]])
{
cell = (NamesViewCustomCell *)currentObject;
break;
}
}
}
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
// Configure the cell.
[[cell nameName2] setText:[managedObject valueForKey:@"nameContent"]];
return cell;
}
if (selectedUnit == 1) {
NSManagedObject *managedObject = [fetchedResultsController2 objectAtIndexPath:indexPath];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [managedObject valueForKey:@"nameContent"];
return cell;
}
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (selectedUnit == 0) {
RecipeDetailViewController *recipeDetailView = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
AddName *addName = (AddName *)[fetchedResultsController objectAtIndexPath:indexPath];
recipeDetailView.addName = addName;
[self.navigationController pushViewController:recipeDetailView animated:YES];
}
if (selectedUnit == 1) {
RecipeDetailViewController *recipeDetailView = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
AddName2 *addName2 = (AddName2 *)[fetchedResultsController2 objectAtIndexPath:indexPath];
recipeDetailView.addName2 = addName2;
[self.navigationController pushViewController:recipeDetailView animated:YES];
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (selectedUnit == 0) {
if (fetchedResultsController) return fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity =
[NSEntityDescription entityForName:@"AddName"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"timeStamp"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil cacheName:@"cache"];
aFetchedResultsController.delegate = self;
[self setFetchedResultsController:aFetchedResultsController];
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
if (selectedUnit == 1) {
if (fetchedResultsController2 != nil) {
return fetchedResultsController2;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AddName2" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"cache2"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController2 = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController2;
}
return 0;
}
答案 0 :(得分:0)
您需要将其放在- (IBAction)segmentedControlValueChanged
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}