我有两个具有多对一关系的实体。例如Employee <<---> Shop
。当我创建一个新员工时,我可以为它选择一个商店。我取了所有可用的商店,然后从表格视图中选择一个
现在,我想在此表中添加一个新行,以便能够将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];
}
}
非常感谢你!
答案 0 :(得分:2)
这应该这样做。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self projectsList] count] + 1;
}
- (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];
}
if ( indexPath.row == [projectsList count] )
{
cell.textLabel.text = @"None";
cell.accessoryType = ( taskObject.project ) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
}
else
{
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;
if ( indexPath.row == [projectsList count] )
{
taskObject.project = nil;
}
else
{
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];
}
}