如何在设备方向更改时重新排列集合视图单元格

时间:2014-10-16 06:46:57

标签: ios objective-c uicollectionview uicollectionviewlayout

enter image description here

在纵向模式下,集合视图看起来很好。但是当它是横向模式时,细胞不会重新排列......它们会保留在同一个地方。 enter image description here

我添加了autolayout,以便集合视图伸展以填充屏幕,集合视图具有恒定的高度约束。

我的视图层次结构就像这样

查看 - > ScrollView-> ContentView | - >有问题的集合视图(一个上面的通知标签)                                |  | - > Tableview(通知标签后一个)                                |                                | - >收集视图(顶部的黑色空间)

集合视图的代码是

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
 return 14;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView==self.offerCollectionView) {
    sliderCollectionInHomeScreen *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdForSlider"
                                                                                     forIndexPath:indexPath];
    myCell.labelForShopName.text = [[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"shop_name"];
    myCell.labelForOfferName.text = [[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"offer_title"];
    myCell.offerImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.arrayForOfferSlider objectAtIndex:indexPath.row] objectForKey:@"img_url"]]]];

    ;


    return myCell;
}

1 个答案:

答案 0 :(得分:0)

我不知道这是否正确。但这暂时对我有用

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrient{

//[self.categoryCollectionView reloadData];
UICollectionViewFlowLayout * lay =[[UICollectionViewFlowLayout alloc] init];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    [lay setScrollDirection:UICollectionViewScrollDirectionVertical];
}
else{

    [lay setScrollDirection:UICollectionViewScrollDirectionHorizontal];
}


[self.CollectionView setCollectionViewLayout:lay];


}