ios - 可扩展的UICollectionViewCell重新定位问题

标签 ios swift ipad uicollectionviewcell uicollectionviewlayout

我有一个带有可扩展 uicollectionviewcells 的 uicollectionview。 在 iPhone 上,每行有 1 列,没有问题。 但在 iPad 上,行有 2 列,展开单元格后出现问题。

这是单元格屏幕截图。 not expanded

expanded

单击箭头按钮时我正在重新加载项目

self.collectionView.reloadItems(at: [indexPath])

项目大小函数

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let height = store.pickList[indexPath.row].expanded ? CGFloat(414) : CGFloat(168)
    var width = CGFloat(0)

    if Constants.isPad { width = (self.view.frame.size.width - 25 - 12 - 12) / 2 }
    else { width = self.view.frame.size.width - 12 - 12 }

    return CGSize(width: width, height: height)
}

CellforRowAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "detailCell", for: indexPath) as! DetailCollectionViewCell


    .....
    if data.expanded {
        UIView.animate(withDuration: 0.25, animations: {
            cell.bottomView.frame = CGRect(x: 0, y: 372, width: cell.frame.width, height: 42)
            cell.expandArrow.setBackgroundImage(UIImage(named: "ok2"), for: .normal)
        }, completion: { result in
            UIView.animate(withDuration: 0.2, animations: {
                cell.detailView.alpha = 1
            })
        })
    }
    else {
                    UIView.animate(withDuration: 0.25, animations: {
            cell.detailView.alpha = 0
        }, completion: { result in
            UIView.animate(withDuration: 0.2, animations: {
                cell.expandArrow.setBackgroundImage(UIImage(named: "ok"), for: .normal)
                cell.bottomView.frame = CGRect(x: 0, y: 124, width: cell.frame.width, height: 42)
            })
        })
    }

    return cell
}

当我展开单元格时,行也会展开。 我的实现有什么问题吗? 谢谢

最佳答案

发生这种情况是因为当单元格显示在iPad。

仅针对 iPad 将计算值除以 2,从而为 UICollectionView 提供足够的空间来在同一行中显示 1 个以上的单元格。对于其他设备,您没有除以 2 的功能,这样只能在一行中显示 1 个单元格。

因此,您必须更新计算以删除除以 2 的结果,或者,如果在这种情况下,对于 iPad,您需要单元格仅为计算值的一半,请实现 minimumInterItemSpacing委托(delegate)方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    if Constants.isPad {
        // Replace this with whatever that suits your need.
        return 70.0
    } else {
        // Replace this with whatever that suits your need.
        return 10.0
    }
}

这将为两个连续的项目提供足够的空间,以便仅在一行中显示 1 个项目。您可以修改上述值以使单元格按您想要的方式显示。

关于ios - 可扩展的UICollectionViewCell重新定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52893698/

相关文章:

iPhone 应用程序外部显示窗口在 iPad 上运行时放大/缩小

ios - mtouch --device=VALUE 选项可接受的值是什么?

javascript - Apple 设备中的复选框无法通过带有 js/jquery 的按钮进行检查

ios - A/B 测试平台如何即时替换 Objective-C Assets ?

ios - 尝试获取 UIDevice.current.identifierForVendor?.uuidString 时崩溃

ios - 我在 firebase 中的数据库结构会影响加载速度吗?

ios - Realm 中列表的默认值

iphone - applicationDidBecomeActive 返回首页

ios - 如何从另一个 JSON 格式的字符串中提取一个字符串?

ios - Swift 无法将类型 'CGPath' 的值分配给类型 'CGPath?' 的值