【问题标题】:How to rearrange collection view cell when device orientation changes设备方向更改时如何重新排列集合视图单元格
【发布时间】:2014-10-16 06:46:56
【问题描述】:

在纵向模式下,收藏视图看起来不错。但是当它是横向模式时,单元格不会重新排列......它们保持在同一个地方。

我添加了自动布局,以便集合视图伸展以填满屏幕,并且集合视图具有恒定的高度约束。

我的视图层次结构是这样的

View ->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;
}

【问题讨论】:

  • 分享一些代码,它是否给出任何错误或警告?
  • 它没有显示任何错误。
  • 分享一些收藏视图的代码。

标签: ios objective-c uicollectionview uicollectionviewlayout


【解决方案1】:

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

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


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多