testing
testing

Reputation: 20289

Insert sections at the beginning of UICollectionView

I want to add some sections to UICollectionView. insertSections at the index zero didn't work for me. So my idea was to insertSections at the end and then use moveSection:toSection: to move the elements from the end to the beginning. Here I get

NSInternalInconsistencyException Reason: attempt to move section 36, but there are only 36 sections before the update

I can only provide C# code, but you can also post 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);
        }
    }
);

Edit:

Here is an output of the variables:

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  

Upvotes: 0

Views: 1333

Answers (1)

dan
dan

Reputation: 9825

If you have 36 sections then then your valid section indexes are 0-35, but your loop will end up calling MoveSection(36, 0). You should use < instead of <=.

Upvotes: 1

Related Questions