繁体   English   中英

UICollectionView单元格布局不正确

[英]UICollectionView cell layout not correct

我是UICollectionViews 我正在制作一个universal app 我想要的是在iPad上每行有2个单元。 但是在iPhone上,每行只有一个单元格。 我能够正确设置iPad版本。 但是我无法理解iPhone的设置。

这是到目前为止的设置。 并且您可以在我的storyboard提要中看到它吗? 但是当我运行它时,我看到每行上仍然有两个单元格。

在此处输入图片说明

有人可以帮我弄这个吗 ?

亲切的问候

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"];

        // Configure layout
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake(100, 100)];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [self.collectionView setCollectionViewLayout:flowLayout];


    }
    -(void)viewWillAppear:(BOOL)animated {

        [super viewWillAppear:animated];
        deviceModel = (NSString*)[UIDevice currentDevice].model;
        NSLog(@"device is = %@",deviceModel);
    }
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        if ([deviceModel isEqualToString:@"iPhone Simulator"]) {
            return 1;
        }else
        {
            return 2;
        }
    }
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
          return 3;

    }
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        // Setup cell identifier
        static NSString *cellIdentifier = @"cvCell";

        /* Uncomment this block to use subclass-based cells */
        CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


        if (indexPath.section == 0) {
            cell.backgroundColor = [UIColor redColor];
        }
        if (indexPath.section == 1) {
            cell.backgroundColor = [UIColor blueColor];
        }
        cell.titleLabel.text = [NSString stringWithFormat:@"item %ld",(long)indexPath.section];
        // Return the cell
        return cell;

    }

检查此代码,这将为您在iPhone单元中提供一项,在iPad中为两项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM