UICollectionView not displaying cells after fast scroll
I have a UICollectionView that while scroll in occasions stops updating cells. It load a few cells but when I scroll down or up the rest of the UIcollectionView is empty although the contensize is bigger that the few cells visibles sizes.
It happens more when I scroll really fast, randomly cellForItemAtIndexPath stops being called and the UICollectionView only shows the last loaded cells, with the rest of the space empty.
Here is my code:
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([NewsCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([NewsCollectionViewCell class])];
[self.collectionView registerClass:[NewsHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([NewsHeaderCollectionReusableView class])];
pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return self.newsArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([NewsCollectionViewCell class]) forIndexPath:indexPath];
[cell refreshWithContentModel:self.newsArray[indexPath.item]];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
NewsHeaderCollectionReusableView *collectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([NewsHeaderCollectionReusableView class]) forIndexPath:indexPath];
if (self.newsArray.count) {
[collectionReusableView refreshWithContentModel:self.newsArray[0]];
}
return collectionReusableView;
}
pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.item == self.newsArray.count - 1) {
[self addMoreNews]; // add items to array and call collection reloaddata
}
}
pragma mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(ceilf(self.collectionView.bounds.size.width / 3.0) - 10, ceilf(self.collectionView.bounds.size.height / 3.0));
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(self.collectionView.bounds.size.width, ceilf(self.collectionView.bounds.size.height / 2.0));
}
链接地址: http://www.djcxy.com/p/91646.html
上一篇: 将AppDelegate导入模型?