集合视图单元格的内容大小不正确

时间:2016-07-22 02:11:19

标签: ios swift ipad uicollectionview uicollectionviewcell

我用144个单元格创建了一个集合视图。我有他们设置单选。我现在已经玩了一段时间的设置,但实际上正确显示单元格的任何设置都有这个问题,其中两个特定单元格的大小一致。请注意,问题只发生在iPad专业版上进行测试时。他们会经常在我不能来回转换图像时无法复制的条件下调整大小。我尝试只在绝对必要时调用reloadData(),并在indexPath函数中调用重新加载单个单元格。我已经测试了我能想到的一切,以防止这成为一个问题。任何正确方向的指针都将受到高度赞赏!

这是一张图片: enter image description here

这是我的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell
    cell.setColor(colorArray[indexPath.section][indexPath.row])
    cell.overlay?.image = cell.whichColor.toImage()
    return cell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 24
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 12
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionView, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 0, 0, 0)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let screenRect: CGRect = collectionView.bounds
    let screenWidth: CGFloat = screenRect.size.width
    let cellWidth: CGFloat = screenWidth / 24
    //Replace the divisor with the column count requirement. Make sure to have it in float.
    let size: CGSize = CGSizeMake(cellWidth, cellWidth)
    return size
}

2 个答案:

答案 0 :(得分:1)

我的上一个项目确实遇到了同样的问题。我设法通过使用此库来解决问题:Snapkit。在pod中导入库并在cellForItemAtIndexPath

中实现它
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell

    let width = self.view.bounds.size.width / 24
    cell.overlay.snp_makeConstraints { (make) -> Void in
        make.width.equalTo(width)
        make.height.equalTo(width)
    }
    cell.setColor(colorArray[indexPath.section][indexPath.row])
    cell.overlay?.image = cell.whichColor.toImage()
    return cell
}

只在故事板中的imageView上放置顶部和前导约束。希望它有所帮助。

答案 1 :(得分:0)

我通过执行以下操作解决了问题:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ChartViewCell
    cell.setColor(colorArray[indexPath.section][indexPath.row])
    cell.overlay?.image = cell.whichColor.toImage()

    let screenRect: CGRect = collectionView.bounds
    let screenWidth: CGFloat = screenRect.size.width
    let cellWidth: CGFloat = screenWidth / 24
    cell.singleCellWidthConstraint.constant = cellWidth - 8
    cell.overlayWidthConstraint.constant = cellWidth - 4
    return cell
}

我刚为图像添加了宽度约束,并在单元格类中为它们添加了一个出口。然后我删除了底部和右边的约束,并在图像上添加了1:1约束。它在所有设备上都像是一种魅力!