简体   繁体   中英

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.

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. 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. 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. Try to simplify this also.

Also, from your code it is not clear what the variable cyc is and where it comes from. You might want to choose more human understandable variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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