2013-08-23 4 views
0

UICollectionViewFlowLayout과 함께 프로그래밍 방식으로 만든 UICollectionView가 있습니다. 이 코드는 다음과 같습니다.UICollectionViewFlowLayout을 조정할 때 UICollectionView 프레임 변경시

- (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]" 

가, 및/또는 발생했을 경우도, 내가 말할 충돌을 받고 있어요 이 펑키 한 동작의 일부 또는 전부를 유발할 수있는 명백한 오류가 있습니다.

답변

0

이와 비슷한 것을 구현하고 있습니까?

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { 
    return YES; 
] 
+0

감사합니다 - 내가 바로 다시로드 컬렉션 뷰를 다음 레이아웃의 minimumInterItemSpacing [invalidateLayout를 레이아웃]을 재설정하고 이전에 추가했지만,이 문제를 해결하지 않는 것 같습니다. – jac300

관련 문제