添加为关系 core-data 设置 nil 的功能
我有两个具有多对一关系的实体。例如 Employee <<--->商店
。当我创建一个新员工时,我可以为其选择一个商店。我获取所有可用的商店,然后从表视图中选择一个。
现在,我想在此表中添加一个新行,以便能够将关系设置为 nil,例如通过添加名为 "None"
的行,当选择它时,关系将为 employee.shop = nil;
是否可以?我不知道如何配置表视图来完成这项工作...
但是,这是用于获取商店的代码:
-(NSArray *)projectsList
{
if (!projectsList) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:taskObject.managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *projects = [taskObject.managedObjectContext executeFetchRequest:request error:&error];
if (!projects) {
NSLog(@"Risultati della richiesta nulli!");
abort();
}
projectsList = [projects mutableCopy];
}
return projectsList;
}
和一些 tableView 方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self projectsList] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Project *project = (Project *)[[self projectsList] objectAtIndex:indexPath.row];
cell.textLabel.text = project.title;
if (project == taskObject.project) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Project *project = (Project *)[projectsList objectAtIndex:indexPath.row];
taskObject.project = project;
NSError *error = nil;
if (![taskObject.managedObjectContext save:&error]) {
NSLog(@"Errore nel salvare il progetto per il task! %@, %@", error, [error userInfo]);
abort();
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
非常感谢!
I have two entity with a many-to-one relationship. For example Employee <<---> Shop
. When I create a new employee, I can choose a shop for it. I fetch all the available shops and then I select one from the table view.
Now, I want to add a new row in this table to be able to set nil to the relationship, for example by adding a row called "None"
and when it's selected, the relationship will be employee.shop = nil;
Is it possible? I don'w know how to configure the table view to do this job...
However, this is the code used to fetch the shops:
-(NSArray *)projectsList
{
if (!projectsList) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:taskObject.managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *projects = [taskObject.managedObjectContext executeFetchRequest:request error:&error];
if (!projects) {
NSLog(@"Risultati della richiesta nulli!");
abort();
}
projectsList = [projects mutableCopy];
}
return projectsList;
}
and some tableView methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self projectsList] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Project *project = (Project *)[[self projectsList] objectAtIndex:indexPath.row];
cell.textLabel.text = project.title;
if (project == taskObject.project) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Project *project = (Project *)[projectsList objectAtIndex:indexPath.row];
taskObject.project = project;
NSError *error = nil;
if (![taskObject.managedObjectContext save:&error]) {
NSLog(@"Errore nel salvare il progetto per il task! %@, %@", error, [error userInfo]);
abort();
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到。
This should do it.