我的实体名称是密码,它有 4 个字符串属性,其中之一是“类型”。我希望表格有与类型一样多的部分..我做错了什么?!我没有问题,但表只是空的,部分计数结果为“0”。
我的代码:
-(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
写入数据库效果很好!我有一个记录所有数据库的功能,它很好。
我究竟做错了什么?!?!?!?!?!