【问题标题】:UITableView populating Core data one-to-many relationship using checkmark - CoreData: error: Serious application errorUITableView使用复选标记填充核心数据一对多关系 - CoreData:错误:严重的应用程序错误
【发布时间】:2013-05-02 18:36:14
【问题描述】:

我有一个应用程序,我试图在其中选择程序所依赖的资源(建筑物、IT 系统)。 Entities之间的关系是Resource.resourceusedonprogProgramme.usesresource。数据模型的一部分在这里:

我正在尝试在 UITableViewController 上使用多个复选标记选择,其中特定程序使用了哪些资源。但是,当我尝试与表中的资源单元格交互时,应用程序崩溃并出现错误

CoreData:错误:严重的应用程序错误。异常被捕获 在核心数据更改处理期间。这通常是一个错误 NSManagedObjectContextObjectsDidChangeNotification 的观察者。这 ALL 或 ANY 运算符的左侧必须是 NSArray 或 一个 NSSet。与 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;

        }    }

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

【问题讨论】:

    标签: uitableview core-data relationship


    【解决方案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];
    
    
            }    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多