在设置节插页时,带有FlowLAyout的UICollectionView不滚动

我以编程方式使用UICollectionView。

我已经设置它的框架如下:

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 3000) collectionViewLayout:flowLayout]; 

我的UICollectionViewFlowLayout有一个部分插入设置为:

 flowLayout.sectionInset = UIEdgeInsetsMake(20.0, 20.0, 100.0, 20.0); 

我也只有1节。

我已经将高度设置为3,000,以查看集合视图是否滚动,但不是。 我设置它,因为集合视图似乎没有向上或向下滚动时,我插入新的项目。

UICollectionView是从UIScrollView子类,这意味着我可以保持重置滚动视图的内容大小。 这会正常工作吗?

唯一的问题是,如果我知道所有项目的大小,包括每行有多less项目,这将工作。

如果每个项目有不同的大小,我怎样才能找出每一行的大小? 我将需要添加所有行大小,并增加self.collectionView的内容大小的高度。

编辑

即使我上面的build议,重置内容大小,不能正常工作! 更新我的集合视图时,我尝试了以下方法:

  int numberOfRows = self.dataArray.count / 3; self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, (numberOfRows * 200) + 300); 

这很丑陋。 这是因为它假设我所有的图像大小为200x200px,每行适合3个图像(因此除以3)。

而且,即使是我有的+300,最后一行我也只能看到3/4,而不是全部。 我必须滚动更多,我将能够看到它,但它又回到3/4。 它有点像滚动刷新,只有拖动视图才会移动视图,当你离开它时,视图会回滚到原来的位置。 为什么发生这种情况? 苹果devise的UICollectionView如此糟糕吗? 这是有点荒谬的… UITableViews自动调整他们的滚动根据他们的内容。

在回答关于UICollectionView的一些其他问题时,我创build了这个演示项目,它滚动得很好,所以我不知道你有什么问题。 我唯一需要做的,使最后一行完全可见的是增加底部插入的大小为90.我还添加了一个完成方法performBatchUpdates:完成:自动滚动,使最后添加的项目是可见的请注意,我使用performSelector添加了一些额外的项目; withObject:afterDelay :)。 我的图像是48×48,我只是将我的collections视图设置为我的控制器视图的边界。 这里是代码,所以你可以比较它与你有什么:

 @interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> { NSMutableArray *newData; } @property (nonatomic, retain) UICollectionView *collectionView; @property (nonatomic, retain) NSMutableArray *results; @end @implementation ViewController - (void)viewDidLoad { self.results = [NSMutableArray array]; int i = 11; while (i<91) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT00%d.jpg",i]]; [self.results addObject:image]; i++; } UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; self.collectionView.dataSource = self; self.collectionView.delegate = self; self.view.backgroundColor = [UIColor blackColor]; [self.view addSubview:self.collectionView]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; //[self.collectionView registerClass:[RDCell class] forCellWithReuseIdentifier:@"myCell"]; [self.collectionView reloadData]; [self performSelector:@selector(addNewImages:) withObject:nil afterDelay:3]; } -(void)addNewImages:(id)sender { newData = [NSMutableArray array]; int i = 102; while (i<111) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT0%d.jpg",i]]; [newData addObject:image]; i++; } [self addNewCells]; } -(void)addNewCells { NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; [self.collectionView performBatchUpdates:^{ int resultsSize = [self.results count]; [self.results addObjectsFromArray:newData]; for (int i = resultsSize; i < resultsSize + newData.count; i++) [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; } completion: ^(BOOL finished){ [self.collectionView scrollToItemAtIndexPath:arrayWithIndexPaths.lastObject atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES]; }]; } - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return [self.results count]; } - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { return 1; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; //cell.iv.image = [self.results objectAtIndex:indexPath.row]; cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { UIImage *image = [self.results objectAtIndex:indexPath.row]; return CGSizeMake(image.size.width, image.size.height); } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 10, 90, 10); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Selected Image is Item %d",indexPath.row); } 

UICollectionView的高度设置为3000会使您的滚动问题变得更糟。 如果UICollectionView是3000像素,那么只有在其内容超过3000个像素的情况下才需要滚动。 您应该将其设置为包含的视图的高度(与宽度相同)。

AFAIK也不应该直接设置contentSize ,而是需要在- (CGSize)collectionViewContentSize的子类中重写- (CGSize)collectionViewContentSize方法,但是一般来说不需要为正常使用而改变内容大小。

我不完全清楚你的问题是什么 – 当你添加超过一个屏幕价值的项目,你不能滚动?

我遇到了同样的问题,因为我正在viewWillAppear方法初始化我的collectionView(即它的DataSource)的元素。

我最后添加了一个[self.collectionView reloadData]; 它工作得很好!

也许你是一样的情况。