1

我正在研究应该使用哪种方法将用户的初始输入与以下选定的行链接起来。

目前,我正在向 coredata 实体添加练习,如下所示:

在此处输入图像描述

我想要实现的是将练习添加到上一个 uitableview 中命名的例程中。这涉及用户命名例程组(核心数据实体中的“例程名称”),例如添加到初始核心数据实体的“4 天锻炼”。以下练习添加到具有基于例程组的关系的实体详细信息中。

在此处输入图像描述

我在核心数据模型中创建了一对多关系的关系:

在此处输入图像描述

我将它添加到当前使用的实体中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *messageAlert = [[UIAlertView alloc]
                                 initWithTitle:@"Row Selected" message:@"Added to Routine!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new device
    ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Routines" inManagedObjectContext:context];
    [newDevice setValue: info.name  forKey:@"routinename"];

    NSManagedObject *newDevice1 = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
    [newDevice1 setValue: info.name  forKey:@"name"];

    NSError *error = nil;

    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    // Display Alert Message
    [messageAlert show];

    //Update with checkmark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

基本上我正在研究我应该研究什么方法来实现这一点以及关于代码的任何建议。

我的routineDetailViewController 在“按钮”上有以下内容

- (IBAction)SaveButton:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Routines" inManagedObjectContext:context];
    [newDevice setValue:self.nameTextField.text forKey:@"routinename"];

}

目前他们正在向核心数据添加罚款,但我无法对它们进行分组。

谢谢你。

4

2 回答 2

0

使用 Sensible TableView 框架时,您还应该能够自动对对象进行分组。看看吧,应该可以为你节省大量时间。

于 2013-07-08T01:39:40.950 回答
0

表格视图中的分组是通过以下方法通过“部分”完成的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)documentTableView
      //return number of routines

- (NSInteger)tableView:(UITableView *)documentTableView numberOfRowsInSection:(NSInteger)section
      //return number of routine details for this routine

- (NSString *)tableView:(UITableView *)documentTableView titleForHeaderInSection:(NSInteger)section
      //return name of this routine

您必须考虑如何处理“取消”。不是您在选择表格单元格时插入行。你要“回滚”吗?或者,您可以跟踪另一个数组中的选定行,并在点击“保存”时执行所有数据库工作。

最后,您的 messageAlert 已分配但要释放。它泄漏。

于 2013-07-07T01:41:50.577 回答