UITableView使用复选标记填充核心数据一对多关系-CoreData:错误:严重应用程序错误
我有一个应用程序,我试图选择一个程序所依赖的资源(建筑、IT系统)。实体之间的关系是Resource.resourceusedonprog我找到了答案,答案基于两个问题。首先,对于多对多关系,关系键是NSSet,而不是对象。其次,每个实体的NSManagedObject子类会自动为每个关系生成add和remove方法。在这个例子中,我已经实现了UITableView使用复选标记填充核心数据一对多关系-CoreData:错误:严重应用程序错误,uitableview,core-data,relationship,Uitableview,Core Data,Relationship,我有一个应用程序,我试图选择一个程序所依赖的资源(建筑、IT系统)。实体之间的关系是Resource.resourceusedonprog我找到了答案,答案基于两个问题。首先,对于多对多关系,关系键是NSSet,而不是对象。其次,每个实体的NSManagedObject子类会自动为每个关系生成add和remove方法。在这个例子中,我已经实现了 [资源移除器ResourceUsedOnProgObject:SelectedProgram]和 [resource addResourceusedon
[资源移除器ResourceUsedOnProgObject:SelectedProgram]代码>和
[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];
} }
我找到了答案,它基于两个问题。首先,对于多对多关系,关系键是NSSet,而不是对象。其次,每个实体的NSManagedObject子类会自动为每个关系生成add和remove方法。在这个例子中,我已经实现了
[资源移除器ResourceUsedOnProgObject:SelectedProgram]代码>和
[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];
} }