在将数据保存在核心数据以及行组织时,我遇到了一些问题,为了更好地理解我的问题,我将解释我所拥有的:
我有一个使用动态行的主表视图,在这个表视图中我有一个 + 按钮,每当按下 + 按钮时,如果用户可以选择“单元格类型”插入到主表视图中,就会在弹出窗口中出现一个表视图。“单元格类型”是自定义单元格,它们是一类和 xib 文件。每个自定义单元格都有各种文本字段......所以想法是:
- 选择一种单元格类型并插入到主表视图中。
- 用数据填充文本字段。
- 保存的数据对应于插入的行数和文本字段中的数据。
当调用弹出表视图时,我的主表视图中有这个方法:
- (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;
}
然后在我的popover tableview的didSelectRow中,我有:
- (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;
}
从这里,一个单元格被插入到我的主表视图中,这是我的主表视图相关方法:
- (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;
}
现在,如果我想退出主表视图并转到另一个表视图,我知道我必须将文本字段的内容保存在我的 managedObject 中,所以:
- (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];
}
从这里开始,一行被“保存”并且数量中的文本也......但是当我再添加一行时问题就开始了:
为什么新行出现在 tableview 的顶部,而不是在插入的前一行之后?
当我填充插入的第二行的文本字段(数量)时......退出表格视图并返回......文本字段似乎没有填充......我做错了什么?
如果我一次插入 2 行,则会发生上一个问题,但是如果我插入一个...退出表格视图,然后返回并插入另一行...保存文本字段数量...
我的问题在哪里?它在我的自定义单元格中吗?在哪里?...我很抱歉发了这么长的帖子,但这让我发疯了...
谢谢你的时间
问候