Ios 更新didSelectRowAtIndexPath上的核心数据

Ios 更新didSelectRowAtIndexPath上的核心数据,ios,uitableview,core-data,didselectrowatindexpath,Ios,Uitableview,Core Data,Didselectrowatindexpath,好吧,我已经做了好几天了,似乎不知道我做错了什么 我想要的是在选择行时更新的核心数据。我记下了,但是我希望表视图刷新以显示更改。这就是我的问题所在。我运行: [NSFetchedResultsController deleteCacheWithName:@"Root"]; fetchedResultsController = nil; NSError *error = nil; if(![[self fetchedResultsController] perfor

好吧,我已经做了好几天了,似乎不知道我做错了什么

我想要的是在选择行时更新的核心数据。我记下了,但是我希望表视图刷新以显示更改。这就是我的问题所在。我运行:

    [NSFetchedResultsController deleteCacheWithName:@"Root"];
    fetchedResultsController = nil;
    NSError *error = nil;
    if(![[self fetchedResultsController] performFetch:&error]){

    }
    [[self tabVe] reloadData];
更新数据后,但tableview不更新。我会改变看法,然后回来,但是。。卡住了。我仍然可以滚动并选择更多行,但表格将永远不会刷新。我有一个分段控制器,它可以以我想要的方式改变数据,但我之所以说表被卡住是因为在我选择一行之后,分段控制器不会像选择一行之前那样改变表的内容

我不是在编辑模式下这样做的,我只是希望它在被选中时更新一个值。这是我的密码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self.tabVe deselectRowAtIndexPath:indexPath animated:YES];
        if(cyc == 1){
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                Items *anItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
                anItem.need = @"0";
                NSError *error = nil;
                if(![managedObjectContext save:&error]){

                }
                [NSFetchedResultsController deleteCacheWithName:@"Root"];
                fetchedResultsController = nil;
                NSError *error = nil;
                if(![[self fetchedResultsController] performFetch:&error]){

                }
                [[self tabVe] reloadData];
            }
        }
    }

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"name"] description];
    cell.detailTextLabel.text = [[managedObject valueForKey:@"notes"] description];
    if([[managedObject valueForKey:@"need"] description] == @"0"){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else if([[managedObject valueForKey:@"need"] description] == @"1"){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

他跑了。之后,桌子似乎卡住了。

只是猜测,但我想到了两件事:

  • 试着减少
    didSelectRowAtIndexPath:
    中的代码。根据我的经验,只需将
    fetchedResultsController
    设置为
    nil
    并让延迟加载处理取数即可。另一个想法是根本不使用任何缓存。这是值得一试的,如果它不影响性能,当然是合理的

  • 我注意到您正在用
    ==
    比较两个字符串。我认为您应该使用
    isEqualToString:
    方法进行比较。另外,我不确定为什么除了字符串之外还需要
    description
    方法-一个
    NSString
    description
    是完全相同的字符串。尽量简化这一点


  • 此外,从代码中不清楚变量
    cyc
    是什么以及它来自何处。您可能希望选择更人性化的变量名。

    谢谢您的建议。不幸的是,将
    fetchedResultsController
    设置为
    nil
    并且不使用缓存,无法使表正常工作。另外,我本打算使用
    isEqualToString
    的,但完全忘记了,还是沿用了旧习惯,所以谢谢你提醒我。我不知道为什么要使用
    说明
    ,我就是这么做的。无论如何,感谢您的帮助/建议,但我仍然有相同的问题。您是否在
    fetchedResultsController
    init
    -方法中添加了
    cacheName:nil
    ?是的,我在
    fetchedResultsController
    init
    方法中添加了
    cacheName:nil
    。我制定了自己的解决方法。一点也不合适,但它能满足我的需要。我现在混合使用sqlite3和核心数据。我会把你的答案记为公认的答案,因为它帮助了我:)谢谢。出了什么问题?
    if(![managedObjectContext save:&error]){
    
    }