我想向UICollectionView添加一些部分。索引为零的insertSections对我不起作用。所以我的想法是在结尾处使用insertSections,然后使用moveSection:toSection:将元素从结尾移动到开头。在这里我得到


  NSInternalInconsistencyExceptionException原因:尝试移动第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

最佳答案

如果您有36个部分,则您的有效部分索引为0-35,但是您的循环最终将调用MoveSection(36, 0)。您应该使用<而不是<=

10-05 21:00
查看更多