合成布局和破碎的自定义大小的单元格

时间:2020-04-29 08:31:15

标签: swift uicollectionview uicollectionviewlayout uicollectionviewcompositionallayout

我使用的布局布局相对简单,并且可以正常工作,但是在某些情况下,UICollectionView中的最后一两个单元格的宽度似乎断了。

我的collectionView由具有动态宽度和静态高度的单元格组成。我可以事先计算高度,因为我的单元格基本上是UILabel加上背景UIView加上上/左/右/下约束(每个都有UIPriority(1000))。

With dataWS
    For Each Cell In .Range("B2:B" & LastRow)
        If Cell.Value = Month Then
            If Cell.Offset(0, -1).Value = Initials Then
                Dim answer As Integer
                answer = MsgBox("Er du sikker på at du vil slette registreringen: '" & Format(Month, "[$-da-DK]mmmm") & "' for '" & Initials & "'" _
                                , vbQuestion + vbYesNo + vbDefaultButton2, "Slet indtastning")
                If answer = vbYes Then
                    .Range(.Cells(Cell.Row, Cell.Column), .Cells(Cell.Offset(11, 0).Row, Cell.Column)).EntireRow.Delete

                    MsgBox "Registrering slettet!"
                    Opgaver.ClearContents
                Else
                    Exit For
                Exit For
                End If
            End If
        End If
    Next
End With

以此类推。

我知道UILabel的字体,我知道所有约束的常量,因此很容易预测单元格的高度。

因此,我的布局如下:

    topicBackView = UIView()
    topicBackView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(topicBackView)

    topicLabel = MyLabel()
    topicLabel.translatesAutoresizingMaskIntoConstraints = false
    topicLabel.numberOfLines = 0
    topicLabel.font = cs.usualFont
    topicLabel.textColor = UIColor.black
    topicLabel.textAlignment = .center
    contentView.addSubview(topicLabel)

    let tpp = topicBackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0)
    tpp.priority = UILayoutPriority(1000)
    tpp.isActive = true

问题是,它可以在iPhone 11上的模拟器中运行,但是当我试图在较小的屏幕上模拟该collectionView时,此collectionView似乎已损坏(在我的情况下,是与iPhone兼容的iPad Air第三代)。

在这种情况下,最后一两个单元格的宽度已损坏,我不知道为什么:

Bad one

应如何: Good one (works like a charm)

这是怎么回事?

2 个答案:

答案 0 :(得分:1)

让估计宽度:CGFloat = 200 将其更改为50即可使用

答案 1 :(得分:0)

我用 cell.sizeToFit() - 创建单元格时

然后此代码创建一个组合布局:

    let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(1), heightDimension: .estimated(1))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    
    let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(1), heightDimension: .estimated(1))
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
相关问题