crashing at performBatchUpdates of collection view [closed]

霸气de小男生 提交于 2019-12-04 10:18:25

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
                                  }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!