首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >[[self.fecthedResultsController sections] count ];coredata tableview sections count变为空。为什么?

[[self.fecthedResultsController sections] count ];coredata tableview sections count变为空。为什么?
EN

Stack Overflow用户
提问于 2013-05-01 16:03:09
回答 1查看 200关注 0票数 0

我的实体名是Password,它有4个字符串属性,其中一个是"type“。我希望表中有多少类型就有多少节。我做错了什么?!我没有问题,但是表是空的,部分计数结果是'0‘。

我的代码:

代码语言:javascript
复制
-(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");
}

日志:

代码语言:javascript
复制
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

对数据库的写入工作正常!我有一个功能,记录所有的数据库,它只是很好。

我做错了什么?!

EN

回答 1

Stack Overflow用户

发布于 2013-05-01 16:18:08

您似乎忘记了在获取的结果控制器上调用performFetch:

代码语言:javascript
复制
-(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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16314680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档