【问题标题】:UICollectionView incorrect width in cellsUICollectionView 单元格中的宽度不正确
【发布时间】:2016-05-13 14:27:11
【问题描述】:

每行需要 2 个单元格。

正好在控制器宽度的中间。

在 iPhone 5 的情况下。屏幕宽度为320,每行为160

self.view.frame.size.width => 320.0
midleWidth => 160.0

但是当 CollectionView 被绘制时,单元格被分开,如下图所示:

我的 CollectionView 配置是:

当我将self.view.frame.size.width 作为单元格的宽度时,按预期取屏幕的所有宽度。

【问题讨论】:

  • 已在代码中的任何位置设置单元格间距?

标签: ios objective-c swift uicollectionview uicollectionviewcell


【解决方案1】:

试试下面的代码

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
        "Cell", forIndexPath: indexPath)
    let viewsDictionary = ["View1": collectionView]
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[View1]|", options: [], metrics: nil, views: viewsDictionary))
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[View1]|", options: [], metrics: nil, views: viewsDictionary))

    return cell
}

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
    let cellSize:CGSize = CGSizeMake(self.bounds.width / 2, self.bounds.height)
    return cellSize
}

【讨论】:

  • 感谢 aswer,Works,但对每个单元格返回一个大警告,与布局问题有关。 Unable to simultaneously satisfy constraints<NSLayoutConstraint:0x7fe1fbcc5890 UICollectionView:0x7fe1fc06de00.leading == UIView:0x7fe1fbcc49c0.leadingMargin - 20><NSLayoutConstraint:0x7fe1fbd42870 H:|-(0)-[UICollectionView:0x7fe1fc06de00] (Names: '|':UIView:0x7fe1fbcc49c0 )>
  • 尝试添加此代码 - cell.contentView.frame = cell.bounds cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
【解决方案2】:

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

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

【讨论】:

  • 这一行 (bounds.size.width/320)* 200 在 iphone 5 中始终为 200。-> (320 / 320) * 200 = 200。而且200不在屏幕中间。如果将200 更改为bounds.size.width / 2,则单元格之间的相同空间再次出现
  • 您需要在此处提供单元格宽度,我提供示例值
【解决方案3】:

以上答案不适用于所有设备试试这个

    - (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
    }

【讨论】:

  • 感谢您的回答,但结果相同。相同的空间。
  • 谢谢哥们,但没用。这两个功能与我在My CollectionView config is: 的问题中添加的功能相同。同样尝试,但没有
猜你喜欢
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 2014-09-14
相关资源
最近更新 更多