在UICollectionView的开头插入节

时间:2015-03-31 14:30:54

标签: ios uicollectionview

我想在UICollectionView添加一些部分。零指数insertSections对我不起作用。所以我的想法最后是insertSections,然后使用moveSection:toSection:将元素从结尾移动到开头。我来了

  

NSInternalInconsistencyException原因:尝试移动第36条,但更新前只有36个部分

我只能提供C#代码,但您也可以发布Objective-C / Swift。

this.controller.CollectionView.PerformBatchUpdatesAsync (
    delegate() {    
        nint sectionsBefore = this.controller.CurrentNumberOfSections;
        this.controller.CurrentNumberOfSections += 12;
        this.controller.CollectionView.InsertSections(NSIndexSet.FromNSRange(new NSRange(sectionsBefore,12)));
        for(nint i=sectionsBefore; i<=this.controller.CurrentNumberOfSections; i++){
            this.controller.CollectionView.MoveSection(i,0);
        }
    }
);

修改

以下是变量的输出:

sectionsBefore: 36  
CurrentNumberOfSections: 48  
Range: <NSIndexSet: 0x7a77b9b0>[number of indexes: 12 (in 1 ranges), indexes: (36-47)]  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  

1 个答案:

答案 0 :(得分:1)

如果你有36个部分,那么你的有效部分索引是0-35,但你的循环最终会调用MoveSection(36, 0)。您应该使用<代替<=