2013-05-02 47 views
1

我有一个应用程序,我试图选择程序依赖的资源(建筑,IT系统)。实体之间的关系是Resource.resourceusedonprog < < ---> Programme.usesresource。一片数据模型在这里: model sliceUITableView使用复选标记填充核心数据一对多关系 - CoreData:错误:严重的应用程序错误

我想在一个特定的程序使用资源的UITableViewController上使用多个复选标记选择。然而,当我尝试在表中的资源细胞相互作用的应用程序崩溃与错误

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet. with userInfo (null)

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Resource Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    Resource *resource = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    NSString *fullname = [NSString stringWithFormat:@"%@ %@", resource.hasmanager.firstname, resource.hasmanager.surname]; 
    cell.textLabel.text = resource.name; 
    cell.detailTextLabel.text = fullname; 

    if (resource.resourceusedonprog == selectedProgramme){ cell.accessoryType = UITableViewCellAccessoryCheckmark;} 
    else {cell.accessoryType = UITableViewCellAccessoryNone;} 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 
    Resource *resource = [self.fetchedResultsController objectAtIndexPath:path]; 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     resource.resourceusedonprog = NULL; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     resource.resourceusedonprog = selectedProgramme; 

     } } 

的错误似乎是由资源被触发。 resourceusedonprog = selectedProgramme;和resource.resourceusedonprog = NULL;事件。如果我将它们注释掉,我可以检查并取消选中单元格(但显然,不会更改resource.resourceusedonprog的状态,即我试图实现的目标)。

回答

1

我找到了答案,它基于两个问题。首先,关系密钥是一个NSSet,而不是一个对象,用于多对多的关系。其次,每个实体的NSManagedObject子类自动为每个关系生成添加和删除方法。在这其中,我实现

[resource removeResourceusedonprogObject:selectedProgramme];

[resource addResourceusedonprogObject:selectedProgramme]; 

总体代码的情况下,是在这里:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Resource Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    Resource *resource = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    NSString *fullname = [NSString stringWithFormat:@"%@ %@", resource.hasmanager.firstname, resource.hasmanager.surname]; 
    cell.textLabel.text = resource.name; 
    cell.detailTextLabel.text = fullname; 

    if ([resource.resourceusedonprog containsObject:selectedProgramme]){ cell.accessoryType = UITableViewCellAccessoryCheckmark;} 
    else {cell.accessoryType = UITableViewCellAccessoryNone;} 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 
    Resource *resource = [self.fetchedResultsController objectAtIndexPath:path]; 
    NSLog(@"selectedResource is: %@", resource.name); 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [resource removeResourceusedonprogObject:selectedProgramme]; 
     } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      [resource addResourceusedonprogObject:selectedProgramme]; 


     } } 
相关问题