开发者

Add ability to set nil for a relationship core-data

开发者 https://www.devze.com 2023-04-03 19:18 出处:网络
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 selec

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] object开发者_开发问答AtIndex: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!


This should do it.

- (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];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消