核心数据插入行和保存问题 [英] Core data inserting rows and saving issue

查看:126
本文介绍了核心数据插入行和保存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在保存核心数据以及行组织时遇到一些问题,并且为了更好地了解我的问题是什么,我会解释一下我有:

I´m having some problems when saving data in core data and also with the rows organization and for better understanding of what my problem is, i´m going to explain what i have:

我有一个主tableview使用动态行,在这个tableview我有一个+按钮,每当按下+按钮,一个tableview出现在popover中,用户可以选择单元格的类型在主tableview中插入。 单元格类型是自定义单元格,他们有一个类和xib文件。每个自定义单元格都有各种文本字段...所以想法是:

I have a main tableview working with dynamic rows, in this tableview i have a + button, whenever the + button is pressed, a tableview appears inside a popover were the user can choose the "type of cell" to insert in the main tableview. The "type of cell" are custom cells and they have they´re one class and xib file. Each custom cell has various textfields...so the idea is:


  1. 选择一种类型的单元格并插入主表格。

  2. 保存的数据对应于插入的行数和文本字段中的数据。

当调用popover tableview时,在我的主tableview中有这个方法:

- (IBAction)Add:(id)sender
{

SelectProduct *typeProduct=[[self.storyboard instantiateViewControllerWithIdentifier:@"selectTable"]initWithTableViewTag:self.tableView.tag];
self.popover=[[UIPopoverController alloc]initWithContentViewController:typeProduct];
[popover presentPopoverFromBarButtonItem:buttonAdd permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
typeProduct.popView = self.popover;
typeProduct.cellSelected = self.cellSelected; //cellSelected is core data subclass.
typeProduct.delegate = self;
typeProduct.managedObjectContext = self.managedObjectContext;
}

然后在我的didSelectRow我的popover tableview,我有: / strong>

then in my didSelectRow of my popover tableview, i have:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
row = indexPath.row;

if (row == 0)
{
    cellSelected=[NSEntityDescription insertNewObjectForEntityForName:@"CellSave" inManagedObjectContext:self.managedObjectContext];
    cellSelected.nameCellData = @"Olive";
    cellSelected.amountData = myCostumCell.amount.text;   
}

从这里,一个单元格被插入我的主tableview是我的主要tableview相关方法:

- (void)viewDidLoad
{
[self.tableView registerNib:[UINib nibWithNibName:@"myCostumCellXib" bundle:nil] forCellReuseIdentifier:@"myCostumCell"];

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.managedObjectContext=[appDelegate managedObjectContext];

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) 
{
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

[self fetchedResultsController];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"myCostumCell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.cellSelected = self.cellSelected;
cell.managedObjectContext = self.managedObjectContext;

if (cell == nil)
{
    cell = [[MyCostumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cellSelected = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.nameCell.text = cellSelected.nameCellData;

if ([cellSelected.nameCellData isEqualToString:@"Olive"])
   {
    cell.amount.text = cellSelected.amountData;
    // i have more textfields to assign but i think you understand the rest..
   }
}

我的fetchedResultsController方法:(也有其他的,但他们是标准的东西)

- (NSFetchedResultsController *)fetchedResultsController
{

if (_fetchedResultsController != nil)
{
    return _fetchedResultsController;
}

// Create and configure a fetch request.
NSFetchRequest *fetchRequestCellSave = [[NSFetchRequest alloc] init];
NSEntityDescription *entityCellSave=
[NSEntityDescription entityForName:@"CellSave" inManagedObjectContext:self.managedObjectContext];
[fetchRequestCellSave setEntity:entityCellSave];

// Create the sort descriptors array.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nameCellData" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequestCellSave setSortDescriptors:sortDescriptors];

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestCellSave managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"nameCellData" cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = _fetchedResultsController;

return _fetchedResultsController;


}

现在如果我想退出主tableview并转到另一个tableview,我明白我必须保存textfields在我的managedObject的内容,因此:

Now if i want to exit the main tableview and go to another tableview, i understand that i have to save the content of the textfields in my managedObject, so:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

    cellSelected.amountData = cell.amount.text;

    //i have more textfields to assign but for the example i think you understand
      what i want.

    [self.managedObjectContext save:nil];

}



从这里,一行是保存在数量也...但问题开始,当我再添加一行:

From here, one row is "saved" and the text in the amount also...but the problems start when i add one more row:


  1. 为什么新行出现在顶部

  1. Why the new row appears on top of the tableview, instead of after the previous row inserted?

当我填写插入的第二行的textField(amount)...退出tableview,

When i fill the textField (amount) of the second row inserted...exit the tableview and come back...the textfield doesn´t appears filled..What am i doing wrong?

上一个问题发生,如果我插入2行一次,但是如果我插入一个...退出tableview然后回来并插入另一行... textfield的金额被保存...

The previous issue happens if i insert 2 rows at once, but if i insert one...exit the tableview and then come back and insert another row...the textfield amount is saved...

我的问题在哪里?是在我的自定义单元格类吗? ...

Where´s my problem? is it in my custom cell class? Where?...I´m sorry for the long post, but this is driving me crazy...

感谢您的时间

注意

推荐答案

您需要在输入数据时立即保存数据,视图从显示中删除。一旦一个单元格滚动屏幕,它将被重用或杀死,所以你必须保存你的文本之前,发生。

You need to save the data as soon as it is entered, not just when the view is removed from display. As soon as a cell is scrolled off screen it will be reused or killed so you have to save your text before that happens. Best place is in the text field delegate callback when the text is changed.

当您在保存之前添加2行时,已损坏了内部状态(当第二行被添加时但您尚未保存第一行中的数据)。

When you add 2 rows before saving you have corrupted your internal state (when the second row is added but you haven't yet saved the data from the first row).

您的行按输入的文本排序,因此它取决于文本(或缺少)

Your rows are sorted by entered text so it depends on the text (or lack of) to determine where it appears on screen.

您可能不应该给单元格引用受管对象上下文(而不是MVC)。

You probably shouldn't be giving the cell a reference to the managed object context (not MVC).

您还应该考虑本地和实例变量之间的区别,因为您的代码似乎混淆了它们...

You should also think about the difference between local and instance variables as your code seems to confuse them...

这篇关于核心数据插入行和保存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆