代码之家  ›  专栏  ›  技术社区  ›  testing

在UICollectionView的开头插入节

  •  0
  • testing  · 技术社区  · 11 年前

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

    NSInternalInconstanceException原因:尝试移动第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 回复  |  直到 11 年前
        1
  •  1
  •   dan    11 年前

    如果您有36个节,那么有效的节索引为0-35,但循环将结束调用 MoveSection(36, 0) 。您应该使用 < 而不是 <= .