具有动态高度和一半宽度的UICollectionview单元格

我正在尝试根据给定的文本创建具有一半宽度和动态高度的集合视图(Expected View)。

我正在尝试通过(ViewController中的代码)实现这一目标

Store

然后(UICollectionviewCell中的代码)

Sub EnumerateFoldersInStores()  
 Dim colStores As Outlook.Stores  
 Dim oStore As Outlook.Store  
 Dim oRoot As Outlook.Folder  

 On Error Resume Next 

 Set colStores = Application.Session.Stores  
 For Each oStore In colStores  
   Set oRoot = oStore.GetRootFolder  
   Debug.Print (oRoot.FolderPath)  
   EnumerateFolders oRoot  
 Next  
End Sub 

Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)  
 Dim folders As Outlook.folders  
 Dim Folder As Outlook.Folder  
 Dim foldercount As Integer 

 On Error Resume Next  
 Set folders = oFolder.folders  
 foldercount = folders.Count  
 'Check if there are any folders below oFolder  
 If foldercount Then  
   For Each Folder In folders  
     Debug.Print (Folder.FolderPath)  
     EnumerateFolders Folder  
   Next  
 End If  
End Sub

通过使用上面的代码,我可以实现创建collectionview Actual view(一半的宽度和动态的高度)。但是集合视图单元格的高度不等于相邻的单元格的高度。实际上,此收集视图像元高度应与相邻像元高度(具有最大高度)相同。如何根据相邻像元大小更新像元大小?

预先感谢

fangke45 回答:具有动态高度和一半宽度的UICollectionview单元格

使用UICollectionViewDelegateFlowLayout

  func collectionView(_ collectionView: UICollectionView,layout 
collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: 
IndexPath) -> CGSize 
{
//let padding: CGFloat =  5
    let collectionViewSize = collectionView.frame.size.width / 2
    return CGSize(width: collectionViewSize,height: 2000 )
}
,

在您的CollectionViewCell类中添加以下代码:

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    setNeedsLayout()
    layoutIfNeeded()
    let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
    var newFrame = layoutAttributes.frame
    newFrame.size.height = ceil(size.height)
    newFrame.size.width = UIScreen.main.bounds.width / 2
    layoutAttributes.frame = newFrame
    return layoutAttributes
}
本文链接:https://www.f2er.com/3117266.html

大家都在问