プログラムで作成した 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;
}
このコードは、コレクション ビューを最初に表示するのに問題なく動作します。私の問題は、デバイスが回転したときにフロー レイアウトを変更しようとすることです。ストラットとスプリングを使用してコレクション ビュー フレームの変更を処理し (上記参照)、willAnimateRotationToInterfaceOrientation メソッドで、フロー レイアウトの minimumInterItemSpacing プロパティを調整し、最後にコレクション ビューでデータの再読み込みを呼び出します。そのためのコードは次のとおりです。
- (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]"
ここで何が起こっているのかを絞り込み始めようとしているときに、私のアプローチが正しいかどうか、および/またはこのファンキーな動作の一部またはすべてを引き起こしている可能性のある明らかなエラーがコードにあるかどうかを誰かが教えてくれるかどうか疑問に思っています.