Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView单元格中的宽度不正确_Ios_Objective C_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionView单元格中的宽度不正确

Ios UICollectionView单元格中的宽度不正确,ios,objective-c,swift,uicollectionview,uicollectionviewcell,Ios,Objective C,Swift,Uicollectionview,Uicollectionviewcell,我每行需要2个单元格 精确到控制器宽度的中间 在iphone5手机壳中。屏幕宽度为320,每行为160 self.view.frame.size.width => 320.0 midleWidth => 160.0 但绘制CollectionView时,单元格会分开,如下图所示: 我的CollectionView配置为: 当我把self.view.frame.size.width作为单元格的宽度时,按预期的方式计算屏幕的所有宽度 //像这样使用它将适用于所有设备 - (CGSi

我每行需要2个单元格

精确到控制器宽度的中间

在iphone5手机壳中。屏幕宽度为
320
,每行为
160

self.view.frame.size.width => 320.0
midleWidth => 160.0
但绘制CollectionView时,单元格会分开,如下图所示:

我的CollectionView配置为:

当我把
self.view.frame.size.width
作为单元格的宽度时,按预期的方式计算屏幕的所有宽度


//像这样使用它将适用于所有设备

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size;
    CGRect bounds = [[UIScreen mainScreen] bounds];
    double bWidth = (bounds.size.width/320)* 200 ; // 200 is your cell width which given in xib
    double bheight = (bWidth/320)* 250 // 250 is your cell height which given in xib;
    size = CGSizeMake(bWidth, bheight);

    return size;
}

//像这样使用它将适用于所有设备

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size;
    CGRect bounds = [[UIScreen mainScreen] bounds];
    double bWidth = (bounds.size.width/320)* 200 ; // 200 is your cell width which given in xib
    double bheight = (bWidth/320)* 250 // 250 is your cell height which given in xib;
    size = CGSizeMake(bWidth, bheight);

    return size;
}

以上答案不适用于所有设备。请尝试此方法

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

            CGSize windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size;

             return CGSizeMake(windowSize.width/2,heightOfCell);
        }
    - (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0
}

    - (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0
    }

以上答案不适用于所有设备。请尝试此方法

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

            CGSize windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size;

             return CGSizeMake(windowSize.width/2,heightOfCell);
        }
    - (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0
}

    - (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0
    }
试试下面的代码

试试下面的代码


谢谢你的回答,但结果是一样的。同样的空间。谢谢,伙计,但不行。这两个函数与我在
我的CollectionView配置中的问题中添加的函数相同:
。同样地,尝试一下,但除了相同的结果,什么也不想得到答案。同样的空间。谢谢,伙计,但不行。这两个函数与我在
我的CollectionView配置中的问题中添加的函数相同:
。同样尝试一下,但是没有这一行
(bounds.size.width/320)*200
在iphone 5中始终是200。-><代码>(320/320)*200=200。200不是屏幕的中间。如果将
200
更改为
bounds.size.width/2
单元格之间再次出现相同的空格,您需要在此处给出单元格宽度,我给出示例值此行
(bounds.size.width/320)*200
在iphone 5中始终是200。-><代码>(320/320)*200=200。200不是屏幕的中间。如果将
200
更改为
bounds.size.width/2
单元格之间再次出现相同的空格,您需要在此处指定单元格宽度,我将给出示例值hanks for aswer,Works,但会为每个单元格返回一个与布局问题相关的大警告<代码>无法同时满足约束和
尝试添加此代码-cell.contentView.frame=cell.bounds cell.contentView.autoresizingMask=[.FlexibleWidth,.FlexibleHeight]感谢aswer,可以工作,但会为每个单元格返回一个与布局问题相关的大警告<代码>无法同时满足约束和
尝试添加此代码-cell.contentView.frame=cell.bounds cell.contentView.autoresizingMask=[.FlexibleWidth,.FlexibleHeight]是否在代码的任意位置设置了单元格间距?是否在代码的任意位置设置了单元格间距?