【问题标题】:Collection View Cells have incorrect content size集合视图单元格的内容大小不正确
【发布时间】:2016-11-25 19:21:29
【问题描述】:

我制作了一个包含 144 个单元格的集合视图。我将它们设置为单选。我已经使用了一段时间的设置,但是任何实际正确显示单元格的设置都有这个问题,即两个特定单元格的大小始终是错误的。请注意,该问题仅在 iPad pro 上进行测试时才会发生。他们会经常在我无法复制的情况下调整大小,因为他们来回更改他们的图像给其他人。我尝试仅在绝对必要时调用 reloadData(),并在 indexPath 函数中调用重新加载单个单元格。我已经测试了我能想到的一切,以防止这成为一个问题。任何指向正确方向的指针将不胜感激!

这是一张图片:

这是我的代码:

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
}

【问题讨论】:

    标签: ios swift ipad uicollectionview uicollectionviewcell


    【解决方案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 上仅放置顶部和前导约束。希望对您有所帮助。

    【讨论】:

    • 感谢您的回答,但我不希望为这个错误在我的项目中添加第三方库。我会尝试以这种方式设置约束,看看它是否有帮助。我目前将它们连接到细胞的所有侧面。
    • @Sethmr 好的。让我知道您是否可以解决此问题,以便对我的下一个项目有所帮助。非常感谢
    【解决方案2】:

    我通过以下操作解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多