小编raj*_*h s的帖子

仅显示两列,使用故事板在CollectionView中显示多行

我想连续显示两个单元格,无论iPhone的屏幕大小是多少.喜欢, 最终结果要求

我的故事板包含一个UICollectionView由约束连接的故事板.

故事板预览

故事板的设置UICollectionView是, 在此输入图像描述

现在,当我在6,5s或更低版本中运行时,只有一个单元格出现在一行中.我用的代码是,

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [categoryImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath];
    cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用代码检测屏幕大小并以编程方式分配适当的单元格大小,

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize sizes;


    CGSize result = [[UIScreen mainScreen] bounds].size;
    NSLog(@"%f",result.height);
    if(result.height == 480)
    {
        //Load 3.5 inch xib
        sizes =CGSizeMake(170.f, 170.f);
        NSLog(@"3gs");
    }
    else if(result.height == 568)
    {
        //Load 4 inch xib
        sizes =CGSizeMake(130.f, 130.f);
        NSLog(@"5s"); …
Run Code Online (Sandbox Code Playgroud)

storyboard ios uicollectionview uicollectionviewcell

21
推荐指数
2
解决办法
3万
查看次数