繁体   English   中英

我想在所有iphone模型的uicollectionview中的一行中显示两个单元格

[英]i want to show two cell in one row in uicollectionview in all iphone model

这是我尝试使用此代码的一些快照,但无法正常工作:

我的collecionview单元:

大小是:166:32最小间距:10:10

插图部分:20:20

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt
        indexPath: IndexPath) -> CGSize
    {
        if collectionView == MainCollectionView
        {
            let padding : CGFloat =  50
            let collectionViewSize = MainCollectionView.frame.size.width - padding

            return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)

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

在视图中使用UICollectionViewDelegateFlowLayout并调用委托方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = self.view.frame.width
    return CGSize(width: (width)/2, height: (width)/2) // width & height are the same to make a square cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{

    let width = (collectionView.frame.width/2)-0.5
    return CGSize(width:width , height: width)

}

尝试实现以下UICollectionViewDelegateFlowLayout delegate methods以实现所需的结果:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    return CGSize(width: (collectionView.bounds.width - 10 - 20*2)/2, height: collectionView.bounds.height)
    //In collectionView.bounds.width - 10 - 20*2 :
    //10 for cell spacing and 
    //20*2 for section insets on left and right
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
{
    return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
    return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
}

更清楚地说明您的要求,以便我进一步帮助您。

您的代码是完美的,但是您正在使用的委托方法可能没有被调用。 根据您使用的快速语法版本,尝试使用最新的委托方法。 并且还要确保使自己成为集合的观察者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM