简体   繁体   English

更新didSelectRowAtIndexPath上的核心数据

[英]Update Core Data on didSelectRowAtIndexPath

Alright, I've been at this for days and can't seem to figure out what I've been doing wrong. 好吧,我已经待了好几天了,似乎无法弄清楚我做错了什么。

What I want is the Core Data to update when a row is selected. 我要的是选择一行时要更新的核心数据。 I got that down, but then I want the table view to refresh to display the change. 我记下了,但是然后我想要刷新表视图以显示更改。 That is where I am having the problem. 那就是我遇到的问题。 I run: 我跑:

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

    }
    [[self tabVe] reloadData];

after updating the data, but the tableview doesn't update. 更新数据后,但表视图不会更新。 I'll change views and come back, but it is.. stuck. 我将更改视图并返回,但是它被卡住了。 I can still scroll around and select more rows, but the table will never refresh. 我仍然可以滚动并选择更多的行,但是表永远不会刷新。 I have a segmented controller, and it works great changing the data the way I want to, but the reason I say the table gets stuck is because after I select a row, the segmented controller doesn't change the contents of the table like it would before a row was selected. 我有一个分段控制器,它可以按我想要的方式很好地更改数据,但是我说表被卡住的原因是因为在选择一行后,分段控制器不会像这样改变表的内容将在选择行之前执行。

I am not doing this in edit mode, I just want it to update a value on being selected. 我不在编辑模式下执行此操作,我只是希望它在被选择时更新一个值。 Here is my code: 这是我的代码:

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

I am hoping that someone can help me figure this out. 我希望有人可以帮助我解决这个问题。 cyc is an NSInteger showing the current position of the segmented controller, I am currently only updating the item when cyc is equal to 1. cyc是一个NSInteger,显示分段控制器的当前位置,我目前仅在cyc等于1时更新该项目。

Edit: 编辑:

Everything runs smoothly until 一切运行顺利,直到

if(![managedObjectContext save:&error]){

}

is run. 运行。 After that, that is when the table seems to get stuck. 在那之后,那桌似乎卡住了。

Just guessing, but two things come to mind: 只是猜测,但有两点想到:

  1. Try to cut down on the code in didSelectRowAtIndexPath: in my experience it is enough to simply set the fetchedResultsController to nil and let the lazy loading take care of the fetch. 尝试减少didSelectRowAtIndexPath:的代码didSelectRowAtIndexPath:以我的经验,只需将fetchedResultsController设置为nil并让延迟加载处理提取就足够了。 Another idea is to not use any cache at all. 另一个想法是根本不使用任何缓存。 That would be worth a try, certainly reasonable if it does not hit performance. 如果不影响性能,那将是值得尝试的选择,当然是合理的。

  2. I noticed that you are comparing two strings with == . 我注意到您正在将两个字符串与==进行比较。 I think you should be using the isEqualToString: method for this comparison. 我认为您应该使用isEqualToString:方法进行此比较。 Also, I am not sure why you need the description method in addition to the string - the description of an NSString is exactly the same string. 另外,我不确定为什么除了字符串之外还需要description方法NSStringdescription是完全相同的字符串。 Try to simplify this also. 也尝试简化此过程。

Also, from your code it is not clear what the variable cyc is and where it comes from. 另外,从您的代码中还不清楚变量cyc是什么以及它来自何处。 You might want to choose more human understandable variable names. 您可能想要选择更多人类可以理解的变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM