Trying to insert new items into UICollectionView b

2019-09-06 20:59发布

问题:

My UICollectionView is set to have 3 rows and 3 sections. I am trying to insert a batch (array) 9 of items into the CollectionView, but with the following code below, I get the first 3 things in my batch displayed in all 3 sections.

BATCH: blue, red, green, orange, fish, sand, hat, shoe, black

CURRENT RESULT SHOWN:

blue  blue  blue
red   red   red
green green green 

CURRENT CODE:

...

[self.selectedSearches addObject:string];
        [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.selectedSearches.count-1 inSection:0]]];
 ...

Not sure what I am doing wrong.

回答1:

The issuse is I had

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 3;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 3;
}

Should be

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.arrayOfThings.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}