集合视图单元格的内容大小不正确 [英] Collection View Cells have incorrect content size

查看:135
本文介绍了集合视图单元格的内容大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a collection view with 144 cells. I have them setup for single selection. I have been playing with the settings I have for a while now, but any setup that actually displays the cells correctly has this problem where two specific cells consistently are the wrong size. Note that the problem only happens when testing on the iPad pro. They will resize often under conditions that I can't replicate when changing their image to others back and forth. I try to only call reloadData() when absolutely necessary, and call the reload individual cell at indexPath function. I have tested about everything I can think of to prevent this from being an issue. Any pointers in the right direction would be highly appreciated!

这是一张图片:

Here is an image:

这是我的代码:

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
}


推荐答案

我通过执行以下操作修复了问题:

I fixed the problem by doing the following:

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约束。它就像所有设备上的魅力一样!

I just added a width constraint for the images, and added an outlet to them in the cells class. Then I deleted the bottom and right constraints and added a 1:1 constraint on the images. It worked like a charm on all devices!

这篇关于集合视图单元格的内容大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆