4

i have a collection view which display two array of images with different section count's this will be toggled between two different view of one collectionview view

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (self.storyview)
    {
        return 4;
    }
    else
    {
        return 1;
    }
}

Where i perform Batch updates of collection view. it is getting crashed because i have two section count's

[self.collectionView performBatchUpdates:^{
        NSRange range = NSMakeRange(0, [self.collectionView numberOfSections]);
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
        [self.collectionView deleteSections:indexSet];
        [self.collectionView insertSections:indexSet];

    }
                   completion:^(BOOL finished) {
                       [self.collectionView reloadData];
                   }];

My Crash log is as below:

 Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionView.m:3901
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the collection view after the update (4) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).'

Can anyone et me know how to avoid this?

Yuchen
  • 24,092
  • 18
  • 133
  • 193
iphonedevkumar
  • 129
  • 1
  • 8
  • The number of sections contained in the collection view after the update (4) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted). – Hot Licks Jan 22 '15 at 00:48
  • @HotLicks i can't figure it out, that's the reason i am here – iphonedevkumar Jan 22 '15 at 01:17
  • Well, you haven't shown us any of the logic, nor have you shown us the data. We can't really help you. – Hot Licks Jan 22 '15 at 01:47

1 Answers1

4

As it says in the log, you have to make sure that the number of sections delete, and insert matches the data source, which in your case, doesn't. So instead, the following is what you are looking for,

- (void)viewDidLoad {
    [super viewDidLoad];
    self.numSection = 4;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.numSection;
}

- (IBAction)onButton:(id)sender
{
    NSIndexSet *indexSet0 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,0)];
    NSIndexSet *indexSet123 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,3)];

    self.numSection = ( self.numSection==1 ) ? 4 : 1;

    [self.collectionView performBatchUpdates:^{
        // No matter what it is, you need to compute the number of cells 
        // that you need to reload, insert, delete before hand

        // Reload the first section
        [self.collectionView reloadSections:indexSet0];

        if ( self.numSection==4 ) {
            // Insert section 1, 2, 3
            [self.collectionView insertSections:indexSet123];
        } else {
            // Delete section 1, 2, 3
            [self.collectionView deleteSections:indexSet123];
        }
    }
                                  completion:^(BOOL finished) {
                                      // You don't need to do anything here
                                  }];
}
Yuchen
  • 24,092
  • 18
  • 133
  • 193