我是这个coredata的新手。为了熟悉关系,我创建了两个实体CategoryList(由属性' categories'组成)和ExpenseList(由属性' amountpent'组成),它们具有多对多的关系。来自' CategoryList'的关系到费用列表'是'营销师'。 第一个表格视图显示了每个类别的总费用,如
food -150
transportation -100
mobile -100
shopping -500
并且第二个tableview显示个人费用,如
food -100
food -50
transportation -100
mobile -50
mobile -50
shopping -200
shopping -100
shopping -200
由于其线程安全,我已经编写了使用对象ID从我的第二个tableview中删除单个费用行的代码。当我从第二个表格视图中删除食物条目时说
food -50
当我导航回第一个tableview时,它应该更新食物并显示
food -100
我已经使用默认通知中心注册了第一个tableview,并且每当删除一个对象时,第一个表视图就会发送通知(由nslog检查)。当第一个tableview收到通知时,它会重新加载tableview中的数据。但是,重新加载tableview时,会显示从下一个tableview中删除其对象的类别的不需要的值。 两个tableviews都由导航控制器连接。
这是第一个tableview的m.file文件。
#import "displayTable.h"
#import "CategoryList.h"
#import "ExpenseList.h"
#import "IncomeList.h"
#import "coreAppDelegate.h"
@interface displayTable ()
@end
@implementation displayTable
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
todaysL = [[todayslist alloc]init];
self.expenseArray = [[NSMutableArray alloc]
initWithObjects:@"0.00",
@"0.00",
@"0.00",
@"0.00",
@"0.00",
@"0.00",
nil];
self.categoryArray = [[NSMutableArray alloc]
initWithObjects:@"food",
@"transportation",
@"fuel",
@"mobile",
@"shopping",
@"others",
nil];
[self populateExpenses];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter]
addObserverForName:NSManagedObjectContextObjectsDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification)
{
NSLog(@"Notification received!");
[self populateExpenses];
// [self viewDidLoad];
[self.tableView reloadData];
}];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark-Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.categoryArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_categoryArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [_expenseArray objectAtIndex:indexPath.row];
return cell;
}
这是我获取数据的功能
-(void)populateExpenses
{
NSManagedObjectContext *managedObjectContexts = [self managedObjectContext];
NSEntityDescription *categorylistEntity = [NSEntityDescription entityForName:@"CategoryList" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:categorylistEntity];
NSError *error =nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:request error:&error];
if(fetchedObjects == nil)
{
NSLog(@"your fetching cause error");
}
else
{
for(CategoryList *categoryL in fetchedObjects)
{
if([categoryL.categories isEqualToString: @"food"] )
{
for(ExpenseList *expenseL in categoryL.expenselist)
{
food = food + expenseL.amountSpent;
//NSLog(@"display food %f",food);
}
NSString *foodString = [NSString stringWithFormat:@"%.2f",food];
[self.expenseArray replaceObjectAtIndex:0 withObject:foodString];
//NSLog(@"foooood%f",food);
}
else if ([categoryL.categories isEqualToString: @"transportation"])
{
for(ExpenseList *expenseL in categoryL.expenselist)
{
transportation = transportation + expenseL.amountSpent;
}
NSString *transportationString = [NSString stringWithFormat:@"%.2f",transportation];
[self.expenseArray replaceObjectAtIndex:1 withObject:transportationString];
}
else if ([categoryL.categories isEqualToString: @"fuel"])
{
for(ExpenseList *expenseL in categoryL.expenselist)
{
fuel = fuel + expenseL.amountSpent;
}
NSString *fuelString = [NSString stringWithFormat:@"%.2f",fuel];
[self.expenseArray replaceObjectAtIndex:2 withObject:fuelString];
}
else if([categoryL.categories isEqualToString: @"mobile"])
{
for(ExpenseList *expenseL in categoryL.expenselist)
{
mobile = mobile + expenseL.amountSpent;
}
NSString *mobileString = [NSString stringWithFormat:@"%.2f",mobile];
[self.expenseArray replaceObjectAtIndex:3 withObject:mobileString];
}
else if([categoryL.categories isEqualToString: @"shopping"])
{
for(ExpenseList *expenseL in categoryL.expenselist)
{
shopping = shopping + expenseL.amountSpent;
}
NSString *shoppingString = [NSString stringWithFormat:@"%.2f",shopping];
[self.expenseArray replaceObjectAtIndex:4 withObject:shoppingString];
}
else if ([categoryL.categories isEqualToString:@"others"])
for(ExpenseList *expenseL in categoryL.expenselist)
{
others = others + expenseL.amountSpent;
}
NSString *othersString = [NSString stringWithFormat:@"%.2f",others];
[self.expenseArray replaceObjectAtIndex:5 withObject:othersString];
NSLog(@"%@",othersString);
}
//NSLog(@"count %d", self.expenseArray.count);
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
但是当我退出并重启我的应用程序时,所有内容都会按原样更新,但在删除后不会立即更新。我无法理解为什么。此外,我不知道这是否是使用coredata做事的正确方法。如果有人可以指导我,请提前致谢
答案 0 :(得分:0)
是的,你错过了核心数据的一些核心概念,这意味着你所做的工作比你需要的要多得多。
您拥有数据库中顶级类别的名称,但是您不知道将要获取它们的顺序,因此您创建一个单独的数组并执行一系列复杂的操作来获取值来自数据库的顺序相同。更好的方法是只在数据库中添加一个'displayOrder'字段,并按字段添加一个按顺序。
完全摆脱categoryArray属性,在populateExpenses函数中,只需执行fetch(带有sort子句)。将fetchedObjects保存到数组属性。
首先需要进行抓取以获取顶级对象,但是一旦拥有它们,很少需要进行更多抓取。
在你的cellForRowAtIndexPath中:
CategoryList * category = [_fetchedObjects objecatAtIndex:indexPath.row]; cell.textLabel.text = category.categories; cell.detailTextLabel.text = [[category valueForKeyPath:@“expenseselist。@ sum.amountSpent”] description];
您无需预先取这个,您可以在显示数据时执行此操作。
更新对象以确保注意到以下内容: -
for ( NSManagedObject* object in self.categoryArray) {
[self.managedObjectContext refreshObject: object mergeChanges:YES];
}
在大多数情况下,这样做是告诉对象下次请求它时需要重新更新它的值。
希望这有帮助。
答案 1 :(得分:0)
我通过在将数据存储到数组后清除浮点变量值来解决它。因为我之前没有清除浮点变量,在重新加载表视图时,浮点变量中的先前值也会再次添加,从而产生不需要的值。 所以在viewWillAppear方法中,我编写了重载表视图并清除了浮点变量内容,并且工作正常。