2013-05-01 53 views
0

我的實體名稱是Password,它有4個字符串屬性,其中之一是「type」。 我想表中有多少部分,因爲有類型.. 我做錯了什麼?! 我沒有問題,但表只是空的,部分計數結果'0'。[[self.fecthedResultsController sections] count]; coredata tableview部分計數變爲空。爲什麼?

我的代碼:

-(NSFetchedResultsController*)fecthedResultsController 
{ 

    AppDelegate *delegate = [UIApplication sharedApplication].delegate; 
    NSManagedObjectContext *mainViewcontrollerLocalContext = delegate.managedObjectContext; 

    if(fecthedResultsController!=nil) 
    { 
     NSLog(@"fecthedResultsController1"); 
     return fecthedResultsController; 
    } 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Password" inManagedObjectContext:mainViewcontrollerLocalContext]; 
    [fetchRequest setEntity:entity]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES]; 
    //here is the problam 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    fecthedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:mainViewcontrollerLocalContext sectionNameKeyPath:@"type" cacheName:nil]; 
    fecthedResultsController.delegate=self; 
    NSLog(@"fecthedResultsController2"); 
    return fecthedResultsController; 

} 
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tblMain beginUpdates]; 
} 

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tblMain endUpdates]; 
} 

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    UITableView*tableview=self.tblMain; 
    NSLog(@"didChangeObject"); 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete:[tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
                  withRowAnimation:UITableViewRowAnimationFade]; 

      break; 
     case NSFetchedResultsChangeUpdate:{ 
      Password*p=[self.fecthedResultsController objectAtIndexPath:indexPath]; 
      UITableViewCell*cell=[tableview cellForRowAtIndexPath:indexPath]; 
      cell.textLabel.text=p.userName; 
      cell.detailTextLabel.text=p.password; 
//   cell.imageView.image=[UIImage imageWithData: p.photodata]; 
     } 
      break; 
     case NSFetchedResultsChangeMove: 
      [tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      [tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
     break; } 
} 

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger) sectionIndex forChangeType: (NSFetchedResultsChangeType)type 
{ 
    NSLog(@"didChangeSection"); 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tblMain insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete:[self.tblMain deleteSections:[NSIndexSet 
                      indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
     default: 
      break; 
    } 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    NSLog(@"numberOfSectionsInTableView %i",[[self.fecthedResultsController sections]count]); 
    return [[self.fecthedResultsController sections]count]; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id<NSFetchedResultsSectionInfo>secInfo=[[self.fecthedResultsController sections]objectAtIndex:section]; 
    NSLog(@"numberOfRowsInSection"); 
    return [secInfo numberOfObjects]; 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    // Configure the cell... 
    Password*pass=[self.fecthedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text=pass.userName; 
// cell.detailTextLabel.text=pass.Password; 
    NSLog(@"cellForRowAtIndexPath"); 

    return cell; 
} 

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection: 
(NSInteger)section 
{ 
    return [[[self.fecthedResultsController sections]objectAtIndex:section]name]; 
    NSLog(@"cellForRowAtIndexPath"); 
} 

日誌:

2013-05-01 10:43:59.038 passwordCore[36050:c07] fecthedResultsController2 
2013-05-01 10:43:59.038 passwordCore[36050:c07] numberOfSectionsInTableView 0 
2013-05-01 10:43:59.039 passwordCore[36050:c07] fecthedResultsController1 

寫入到數據庫中運行良好!我有一個功能,記錄所有的數據庫,它就好了。

我做錯了什麼?!?!?!?!?!

回答

0

看來你忘了打電話獲取的成果控制器上performFetch:

-(NSFetchedResultsController*)fecthedResultsController 
{ 
    // ... 

    fecthedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:mainViewcontrollerLocalContext sectionNameKeyPath:@"type" cacheName:nil]; 
    fecthedResultsController.delegate=self; 

    // Insert this code: 
    NSError *error; 
    if (![fecthedResultsController performFetch:&error]) { 
     NSLog("Error: %@", [error localizedDescription]); 
    } 

    NSLog(@"fecthedResultsController2"); 
    return fecthedResultsController; 
} 
+0

而這就是soposd幫助? – 2013-05-01 16:45:10

+0

@RinatMunyAdi:你*有*一次調用'performFetch'。你試過了嗎? – 2013-05-01 16:46:02

+0

沒有。 :/ 我怎麼做? – 2013-05-02 08:01:10