Trying to insert new items into UICollectionView but getting the same cells over and over

萝らか妹 提交于 2019-12-25 07:04:04

问题


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;
}


来源:https://stackoverflow.com/questions/24961081/trying-to-insert-new-items-into-uicollectionview-but-getting-the-same-cells-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!