Swift如何根据内容制作uicollectionviewcell的高度变化

时间:2017-08-01 18:47:02

标签: swift

我有这个Swift代码

Sub TruncateNulls()
    Dim rg As Range, data()

    ' select the range to the last row
    Set rg = Range(Range("A2"), Range("A" & rows.Count).end(xlUp))

    ' load the data in an array
    data = rg.value

    ' replace each value with a regex
    ArrayReplace data, pattern:="\s*null$", replacement:=Empty

    ' write the array back to the sheet
    rg.value = data
End Sub


Sub ArrayReplace(data(), pattern As String, replacement As String)
    Dim re As New RegExp, r As Long, c As Long

    re.Global = True
    re.MultiLine = False
    re.IgnoreCase = True
    re.pattern = pattern

    For c = 1 To UBound(data, 2)
        For r = 1 To UBound(data, 1)
            If VarType(data(r, c)) = vbString Then
                data(r, c) = re.Replace(data(r, c), replacement)
            End If
        Next
    Next
End Sub

我的目标是根据内容自动调整单元格的高度。我想实现 let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: UICollectionViewFlowLayout()) collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.register(PostCell.self, forCellWithReuseIdentifier: cellId) collectionView.delegate = self collectionView.dataSource = self func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: self.view.frame.size.width-30, height: 50) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 15, left: 5, bottom: 15, right: 5) } ,但我不知道self.flowLayout.estimatedSize = UICollectionViewFlowLayoutAutomaticSize是什么。

1 个答案:

答案 0 :(得分:2)

如果你想获得flowLayout:

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.estimatedItemSize = CGSize(width: 100, height: 100) //use auto layout for the collection view
}

您可以这样做:

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.estimatedItemSize = CGSize(width: 100, height: 100)
let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: layout)