Perky
Perky

Reputation: 43

Change a UITableView row's section at runtime

I have a UITableView that is controlled by a NSFetchedResultsController. Rows are organised into sections from a property of the row. But when I modify that property the application terminated with:

Invalid update: invalid number of sections.

Each row represents a shot in a film, wide shot, close up etc.. The row also contains which scene it belongs to and the sections are calculated from that.

Shot.m (CoreData entity)

#pragma mark Transient properties

- (NSString *)sectionIdentifier 
{
    return [self sceneNumber];
}

ShotsViewController.m (The UITableView)

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

I don't know what i'm supposed to do here. I've played around with the UITableView insertSections and deleteSections but I always get same invalid number of sections error.

Edit

it moves automatically to other existing sections:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

It worked after I added the

case NSFetchedResultsChangeUpdate:
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

But now, say i've got two cells. Cell1.sceneNumber = 1 Cell2.sceneNumber = 2

If I set Cell1.sceneNumber = 3 I get the error:

Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (2)

Upvotes: 3

Views: 2830

Answers (1)

skue
skue

Reputation: 2077

We'd need to see a lot more code to know what the problem is. However, one thing that might be useful is checking out the "Mastering Table Views" video from last year's WWDC. They go over the specifics of how to add/remove sections/rows and the order which you should make the change in your model/table (with clear diagrams).

Go to https://developer.apple.com/videos/wwdc/2010/ and after logging in, go to Application Frameworks > Session 128 - Mastering Table Views.

Upvotes: 3

Related Questions