mse*_*sec 0 iphone core-data objective-c nsfetchedresultscontroller ios
嗨我希望能够在我的CoreData数据库中对一个属性(rowTotal)求和,之后它被我在获取结果控制器中的谓词过滤掉了.结果将是订单的小计.
最初我以为我可以在某种程度上只是总结tableCells中显示的值,但我已经读到这不能很好地工作,因为可能只有10个tableCells用于100个项目.
我已经阅读了一个类似的问题(链接如下),该问题涉及将总和放入节标题中.我想要一个变体,我希望在managedObjectContext发生任何更改后,显示和更新总和为UILabel.
我试过以下代码:
float subtotal = 0;
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
NSNumber *objectRowTotalNumber = [object valueForKey:@"rowTotal"];
float objectRowTotal = [objectRowTotalNumber floatValue];
subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);
Run Code Online (Sandbox Code Playgroud)
我已经在我的代码中的多个位置尝试过,看看我是否可以让它工作.我没有成功,并且使用此代码在tableView中不显示数据.我认为它的下面一行给了我悲伤.
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects])
Run Code Online (Sandbox Code Playgroud)
我感觉它实际上创建了一个新的托管对象并干扰了我所拥有的对象.
这是我获取的结果控制器的代码
- (NSFetchedResultsController *)fetchedResultsController {
NSLog(@"I'm inside the method fetchedResultsController");
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
OrderAppDelegate *appDelegate =
(OrderAppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Set the Predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"orderNumber == %@", orderNumberLabel.text];
NSLog(@"predicate is: %@",predicate);
[fetchRequest setPredicate:predicate];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
Run Code Online (Sandbox Code Playgroud)
我是否为此应用程序使用了正确的代码?你能看到我做错的事吗?我应该在哪里实现代码?
非常感谢任何指导,谢谢.
Mar*_*rra 10
顺便说一句,你可以改变这个:
float subtotal = 0;
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
NSNumber *objectRowTotalNumber = [object valueForKey:@"rowTotal"];
float objectRowTotal = [objectRowTotalNumber floatValue];
subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);
Run Code Online (Sandbox Code Playgroud)
对此:
CGFloat subtotal = [[[[self fetchedResultsController] fetchedObjects] valueForKeyPath:@"@sum.rowTotal"] floatValue]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2630 次 |
| 最近记录: |