0

我有一个以编程方式创建的 UICollectionView 及其 UICollectionViewFlowLayout。代码如下:

- (UICollectionView *)createCollectionToDisplayContent:(NSArray *)array ViewWithCellIdentifier:(NSString *)cellIdentifier ofWidth:(CGFloat)width withHeight:(CGFloat)height forEmailView:(EmailView *)emailView
 {

 CGFloat minInterItemSpacing;

 if (!self.orientationIsLandscape) minInterItemSpacing = 9.0f;
    else minInterItemSpacing = 20.0f;

 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
     layout.itemSize = CGSizeMake(220.0f, 45.0f);
     layout.sectionInset = UIEdgeInsetsMake(5.0f, 0.0f, 5.0f, 0.0f);
     layout.minimumLineSpacing = 10.0f;
     layout.minimumInteritemSpacing = minInterItemSpacing;

    //get pointer to layout so I can change it later 
    emailView.personLabelsLayout = layout;

 UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(40, 4, width, height) collectionViewLayout:layout];
     collectionView.dataSource = emailView;
     collectionView.delegate = emailView;
     collectionView.scrollEnabled = NO;
     collectionView.userInteractionEnabled = YES;
     collectionView.backgroundColor = [UIColor clearColor];
     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
     collectionView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin);
     [collectionView reloadData];

 return collectionView;
 }

此代码可以正常工作以最初显示集合视图。我的问题是在设备旋转时尝试更改流程布局。我使用struts和springs(见上文)处理collection view frame的变化,在willAnimateRotationToInterfaceOrientation方法中,我调整了flow layout的minimumInterItemSpacing属性,最后在collection view上调用reload data。这是代码:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{       

CGFloat minInterItemSpacing;

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    minInterItemSpacing = 20.0f;
}  else {
       minInterItemSpacing = 9.0f;
   }

self.visibleEmailView = //...more code
self.visibleEmailView.personLabelsLayout.minimumInteritemSpacing = minInterItemSpacing;
[self.visibleEmailView.personLabelsCollectionView reloadData];    

//other stuff...  

}

最终,当视图旋转时,流布局应该显着调整:根据 minimumInterItemSpacing 以及它显示的列数。但是,布局并没有像我预期的那样调整。我很难弄清楚到底发生了什么。看起来 minimumInterItemSpacing 没有在旋转时被重置,并且看起来对于每个单元格,同一个单元格被多次绘制在自身上。另外,我遇​​到的崩溃说:

"the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path <NSIndexPath 0x9be47e0> 2 indexes [0, 6]"

在试图开始缩小这里发生的事情时,我想知道是否有人可以告诉我我的方法是否正确,和/或我的代码中是否存在可能导致部分或全部这种时髦行为的明显错误.

4

1 回答 1

0

你正在实施这样的事情吗?

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
]
于 2013-08-23T22:11:54.160 回答