UICollectionViewListCell 附件 TopTrailing 放置

UICollectionViewListCell Accessory TopTrailing Placement

iOS 14 介绍了 UICollectionView 的一系列改进,包括新的 UICollectionViewListCell。使用 defaultContentConfiguration,您可以向单元格添加附属视图。我正在寻找重新创建一个 iMessage 对话行(邮件也关闭),其中日期标签位于顶部尾角。反正有没有使用默认配置来做到这一点?为此必须创建一个自定义单元似乎有点矫枉过正。

这是我目前拥有的。

let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
    var content = cell.defaultContentConfiguration()
    content.text = "Title"
    content.secondaryText  = "This is the body of the message and it's really long. I want to see where it finally truncates because thats what it should do eventually you know?"
    content.secondaryTextProperties.numberOfLines = 2
    content.image = UIImage(systemName: "star")
    
    
    let label = UILabel()
    label.text = "4/26/7"
    label.textColor = .secondaryLabel
    label.font = .preferredFont(forTextStyle: .caption1)
    
    let customAccessory = UICellAccessory.CustomViewConfiguration(
        customView: label,
        placement: .trailing(displayed: .always))
    
    cell.accessories = [.customView(configuration: customAccessory)]
    cell.contentConfiguration = content
    cell.tintColor = .tertiaryLabel
}

这是我想要的结果

这是另一个例子的邮件

默认 UICollectionViewListCell 的自定义可能性有限。

你最好的选择是使用自定义 UICollectionViewCell subclass。相反,您将让单元注册注册您的自定义 class,并以与内置 UICollectionViewListCell class 对象出列相同的方式出列单元。