Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 我需要第二个NSFetchedResultsController吗_Uitableview_Ios5_Core Data - Fatal编程技术网

Uitableview 我需要第二个NSFetchedResultsController吗

Uitableview 我需要第二个NSFetchedResultsController吗,uitableview,ios5,core-data,Uitableview,Ios5,Core Data,我的应用程序的一个功能使用核心数据作为属性存储用户的费用。我试图在表视图控制器的第一部分显示所有这些费用的总和 当总和显示在我表格的第一部分时,我已经让它工作得很好了。然而,当我使用第0节时,它只是中断。我已经调试了这个应用程序,以找出它在哪里坏了,为什么坏了。我发现在两次调用fetchedResultsController时出现了问题 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger

我的应用程序的一个功能使用核心数据作为属性存储用户的费用。我试图在表视图控制器的第一部分显示所有这些费用的总和

当总和显示在我表格的第一部分时,我已经让它工作得很好了。然而,当我使用第0节时,它只是中断。我已经调试了这个应用程序,以找出它在哪里坏了,为什么坏了。我发现在两次调用fetchedResultsController时出现了问题

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if (section == 1) {
            return 1;       
        }
        if (section == 0){
            return [self.fetchedResultsController.fetchedObjects count]; 
        }
        else return 0;
    }

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {

        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gastos" inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nombre" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    _fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![_fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}
以下代码将不起作用,因为已为sum创建了_fetchedResultsController,并且不会传递if(_fetchedResultsController!=nil)

我是否需要为求和使用另一个NSFetchedResultsController?不管是这样,你会怎么做?谢谢

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0) {
            return 1;
        } else {
            return [[self.fetchedResultsController fetchedObjects] count];
            //id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        }   
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0) {
            static NSString *ct = @"CellSum";      
            UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ct];
            [self configureCell:cell atIndexPath:indexPath];
            return cell;
        } else {
            static NSString *ci = @"Cell";      
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ci];
            [self configureCell:cell atIndexPath:indexPath]; 
            return cell;       
        }

    }
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

        //Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSNumber *sum = [self.fetchedResultsController.fetchedObjects
                         valueForKeyPath:@"@sum.precio"];
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterCurrencyStyle];
        [f setMinimumFractionDigits:2];
        [f setMaximumFractionDigits:2];

        NSString *precio = [f stringFromNumber: sum];

        cell.detailTextLabel.text = [NSString stringWithString:precio];
        cell.textLabel.text = @"Suma Total:";
    } else{
        Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
        cell.textLabel.text = g.categoria.nombre;
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterCurrencyStyle];
        [f setMinimumFractionDigits:2];
        [f setMaximumFractionDigits:2];

        NSString *precio = [f stringFromNumber: g.precio];

        cell.detailTextLabel.text = [NSString stringWithString:precio];
    }

}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
如果(节==0){
返回1;
}否则{
返回[[self.fetchedResultsController fetchedObject]计数];
//id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
}   
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.section==0){
静态NSString*ct=@“CellSum”;
UITableViewCell*单元格=(UITableViewCell*)[self.tableView出列标识符为:ct的可重用单元格];
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}否则{
静态NSString*ci=@“单元格”;
UITableViewCell*单元格=(UITableViewCell*)[tableView出列可重用单元格,标识符:ci];
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
}
-(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath{
//Gastos*g=(Gastos*)[self.fetchedResultsController对象索引路径:indexPath];
if(indexPath.section==0){
NSNumber*sum=[self.fetchedResultsController.fetchedObject
valueForKeyPath:@“@sum.precio”];
NSNumberFormatter*f=[[NSNumberFormatter alloc]init];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setMinimumFractionDigits:2];
[f setMaximumFractionDigits:2];
NSString*precio=[f stringFromNumber:sum];
cell.detailtextlab.text=[nsstringwithstring:precio];
cell.textlab.text=@“Suma总计:”;
}否则{
Gastos*g=(Gastos*)[self.fetchedResultsController对象索引路径:[NSIndexPath indexPathForRow:indexPath.row目录:0]];
cell.textlab.text=g.categoria.nombre;
NSNumberFormatter*f=[[NSNumberFormatter alloc]init];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setMinimumFractionDigits:2];
[f setMaximumFractionDigits:2];
NSString*precio=[f stringFromNumber:g.precio];
cell.detailtextlab.text=[nsstringwithstring:precio];
}
}

原因是获取的结果控制器没有任何节,因此所有行都是零节。您必须修改
cellforrowatinexpath
才能在第1节而不是第0节中工作

您的
单元格中会有类似的内容,如rowatindexpath

NSManagedObject *object = [self.fetchedResultsController 
   objectAtIndexPath:indexPath];
在第1节中,这将不返回任何内容。使用表达式代替上行中的
indexPath

[NSIndexPath indexPathForRow:indexPath.row inSection:0]
它应该在第1节中显示您的
fetchedObjects

至于总和,您只需使用现有的fetch生成它:

NSNumber *sum = [self.fetchedResultsController.fetchedObjects
   valueForKeyPath:@"@sum.nombre"]

您在节中发布了两次
numberofrowsin
,这是不必要的。您没有在索引路径为{0,0}的
cellforrowatinexpath
中发布您的相关部分。Mundi,您的答案成功了!非常感谢你。我更新了上面的问题,添加了解决方案。您的方法很适合显示内容。然而,如果你更深入,你会遇到一些问题。如果进行了插入,则表将出现一种奇怪的行为:case NSFetchedResultsChangeInsert:[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath IndeExpathForRow:newIndexPath.row Instation:1]]withRowAnimation:UITableViewRowAnimationFade];TV部分1将显示前5个获取的结果。但如果我重新启动应用程序,则数据将正确显示。删除和更新可以很好地进行编码,有什么建议吗?没有奇怪的行为。您必须实现NSFetchedResultsController委托方法,并相应地修改它们(即插入/删除具有正确索引路径的行)。请在前面的注释中输入更新的代码。不管怎样,我已经接近你的答案了。干杯