The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (0), plus or minus the number of

现象:当删除CollectionView 当中的某个section的时候,报上面的错误

初步分析:当前CollectionView删除前后都不止一个Section,怎么会报那样的错误;猜想可能是相册界面的另外两个UICollectionView,对当前的CollectionView有影响。

初步验证:当加载了另外一个collectionView,再去删除原collectionView时并没有报错

解决方案:ViewDidLoad的时候加载三个CollectionView

 

再次分析:按道理应该是不会相互影响的,百度了一下之后发现通常出现这类问题是由于执行了增删操作之后,数据源返回的section数据和collectionView中的不一致, 检查代码numberOfSectionsInCollectionView 中 发现多了一个if(HasLoaded){  // do something; } else {  return 0; } 的判断;

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    if (HasLoaded) {
        NSInteger type =0;
        for (NSInteger k=0; k<3; k++) {
            if (collectionView == self.collectionViewArray[k]) {
                type = k;
                break;
            }
        }
        NSInteger count = self.httpConnect.WifiConnected? self.dataModel.ListsArray[type].SectionArray.count: self.dataModel.LocalListsArray[type].SectionArray.count;
        if (count==0 && ![self.collectionViewArray[type] isHidden]) {
            
            self.noDataImageView.hidden = NO;
            self.noDataLabel.hidden = NO;
        }else {
            self.noDataImageView.hidden = YES;
            self.noDataLabel.hidden = YES;
        }
        return count;
    }
    else {
        return 0;
    }
}

当我加载了其他两个CollectionView之后,使HasLoaded为true了,所以之前的分析只是凑巧解决了那个问题。

正确方案:去掉HasLoaded,即可

原文地址:https://www.cnblogs.com/Apple2U/p/5666105.html