UICollectionViewFlowLayout的estimatedItemSize是否会中断滚动?

Adr*_*ian 12 iphone objective-c ios uicollectionview uicollectionviewlayout

(编辑:从iOS 9开始看起来工作正常.我没有进行大量测试,但是这个例子很有效.这证实了iOS 8中存在的错误.)

我花了很多时间测试UICollectionView的Flow Layout自我调整大小的行为.经过很多挫折之后,问题被缩小到这样一个事实:只要将其设置为estimatedItemSize非零大小,滚动就不再正常工作.

在我的示例中,它不显示40个项目,而只显示32个项目.

我复制粘贴下面的代码.我已经测试了许多以Swift版本开头的东西.

基本上它无法计算和/或正确更新布局 collectionViewContentSize()

这是一个完整的演示http://git.io/AIrHNA

有谁能指出我正确的方向?

谢谢

@implementation ViewControllerObjcC

static NSString * const reuseIdentifier = @"Cell";
-(UICollectionViewFlowLayout*)flowLayout{
  return (UICollectionViewFlowLayout*)self.collectionViewLayout;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
  CGSize estimatedSize = CGSizeMake(self.view.frame.size.width, 25.0);
  BOOL testEstimatedItemSize = true;
  if (testEstimatedItemSize) {
    [self flowLayout].estimatedItemSize = estimatedSize;
  }else{
    [self flowLayout].itemSize = estimatedSize;
  }

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 40;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
  [cell.contentView addSubview:label];
  label.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
  label.backgroundColor = [UIColor redColor];
  return cell;
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*ain 0

从文档中:

\n\n
\n

该属性的默认值为CGSizeZero。将其设置为任何其他值都会导致集合视图使用 cell\xe2\x80\x99s\n 方法查询每个单元格的实际大小 preferredLayoutAttributesFittingAttributes:。如果所有单元格的高度相同,请使用该itemSize属性而不是此属性来指定单元格大小。\n

\n
\n\n

preferredLayoutAttributesFittingAttributes:看来你的项目中没有使用这个方法?,你尝试过实现它吗?

\n\n

另一方面,这个解决方案看起来不错: \n使用自动布局指定 UICollectionView 中的单元格的一维

\n