2013-05-02 24 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]; 


     } } 
相關問題