2011-03-28 102 views
1

嗨,大家好,我有一個UISegmented控制器,我試圖用來在從核心數據獲取結果之間切換。但是,當分段控制器更改時,它不會更新UITableView中的獲取結果。我不確定我做錯了什麼。希望有人能指出我正確的方向。這裏是我的代碼: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

您是否迷上了'self.theTableView'表看法?並且'tableSeg'到觸發事件的分段控件?把一個'NSLog(@「選擇已更改:%d%@」,selectedUnit,self.theTableView);'在segmentedControlValueChanged'中查看發生了什麼。 – mvds 2011-03-28 01:27:59

+0

一切正常,但它只是不提取結果。 tableSeg的作品和theTableView的作品。我可以在加載時獲取結果,但是當我切換到另一個實體來獲取這些結果時,它不會將它們加載到tableview中。它只是提取舊的結果。這真的很奇怪。 – 0SX 2011-03-28 01:38:09

回答

0

你需要把這個在您的- (IBAction)segmentedControlValueChanged

if (![[self fetchedResultsController] performFetch:&error]) { 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
}