Изменить раздел строки UITableView во время выполнения - PullRequest
3 голосов
/ 13 марта 2011

У меня есть UITableView, который управляется NSFetchedResultsController. Строки организованы в разделы из свойства строки. Но когда я изменяю это свойство, приложение завершается с:

Invalid update: invalid number of sections.

Каждая строка представляет кадр в фильме, широкий кадр, крупный план и т. Д. В строке также указывается, к какой сцене она относится, и из этого рассчитываются разделы.

Shot.m (сущность CoreData)

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

Я не знаю, что мне здесь делать. Я поиграл с UITableView insertSections и deleteSections, но всегда получаю одно и то же недопустимое количество разделов.

Редактировать

автоматически перемещается в другие существующие разделы:

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

Это сработало после того, как я добавил

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

Но теперь, скажем, у меня две клетки. Cell1.sceneNumber = 1 Cell2.sceneNumber = 2

Если я установлю Cell1.sceneNumber = 3, я получу ошибку:

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)

1 Ответ

3 голосов
/ 13 марта 2011

Нам нужно увидеть намного больше кода, чтобы понять, в чем проблема.Тем не менее, одна вещь, которая может быть полезна, это просмотреть видео «Mastering Table Views» из прошлогодней WWDC.Они описывают особенности добавления / удаления разделов / строк и порядок, в котором вы должны внести изменения в вашу модель / таблицу (с четкими диаграммами).

Перейти к https://developer.apple.com/videos/wwdc/2010/ и послевойдите в систему, перейдите в Application Frameworkworks> Session 128 - Mastering Table Views.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...