添加为关系 core-data 设置 nil 的功能

发布于 2024-12-03 18:41:17 字数 2722 浏览 1 评论 0原文

我有两个具有多对一关系的实体。例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-12-10 18:41:17

这应该可以做到。

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

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文