您如何扩展现代收藏查看单元格默认内容配置?

发布于 2025-02-01 21:18:16 字数 949 浏览 1 评论 0原文

苹果的使用默认的单元格内容配置以设置两行文本,

    func cellRegistrationHandler(cell: UICollectionViewListCell, indexPath: IndexPath, id: String) {
        let reminder = Reminder.sampleData[indexPath.item]
        var contentConfiguration = cell.defaultContentConfiguration()
        contentConfiguration.text = reminder.title
        contentConfiguration.secondaryText = reminder.dueDate.dayAndTimeText
        contentConfiguration.secondaryTextProperties.font = UIFont.preferredFont(forTextStyle: .caption1)
        cell.contentConfiguration = contentConfiguration
    }

我将如何开始将其扩展到包含第三行文本?我想保留内置的文本secondaryText和配件控制(该教程在每个单元格上具有完成按钮),同时还添加了自定义UI元素。我认为这是可能的,因为Apple使用术语“构图集合视图”,但我不确定如何完成此操作。是否有可能,还是需要注册自定义uicollectionViewCell子类?

Apple's Displaying Cell Info tutorial shows using a default cell content configuration to set two lines of text

    func cellRegistrationHandler(cell: UICollectionViewListCell, indexPath: IndexPath, id: String) {
        let reminder = Reminder.sampleData[indexPath.item]
        var contentConfiguration = cell.defaultContentConfiguration()
        contentConfiguration.text = reminder.title
        contentConfiguration.secondaryText = reminder.dueDate.dayAndTimeText
        contentConfiguration.secondaryTextProperties.font = UIFont.preferredFont(forTextStyle: .caption1)
        cell.contentConfiguration = contentConfiguration
    }

How would I get started extending this to include a third line of text? I would like to keep the built-in text, secondaryText, and accessory control (the tutorial has a done button on each cell), while also adding custom UI elements. I'm assuming this is possible since Apple uses the term "compositional collection views," but I'm not sure how to accomplish this. Is it possible, or would I instead need to register a custom UICollectionViewCell subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-02-08 21:18:17

我认为您必须使用自定义uicontentViewuicontentConfiguration对象来完成您在说的内容。

话虽如此,这是内容config/view/collection -list的简化示例 -

// MARK: UIContentConfiguration

struct MyCellContentConfiguration: UIContentConfiguration {
    var text: String?
    
    func makeContentView() -> UIView & UIContentView {
        MyCellContent(configuration: self)
    }
    
    func updated(
        for state: UIConfigurationState
    ) -> MyCellContentConfiguration {
        self
    }
}

// MARK: UIContentView

class MyCellContent: UIView, UIContentView {
    var configuration: UIContentConfiguration {
        get { cellConfiguration }
        set { configureCell() }
    }
    private var cellConfiguration: MyCellContentConfiguration
    private weak var label: UILabel!
    
    init(configuration: MyCellContentConfiguration) {
        self.cellConfiguration = configuration
        super.init(frame: .zero)
        addLabel()
        configureCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addLabel() {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        addSubview(label)
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
            label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            label.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
        ])
        self.label = label
    }
    
    private func configureCell() {
        label.text = cellConfiguration.text
    }
}

// MARK: UICollectionViewController

class MyCollectionViewController: UICollectionViewController {
    typealias DataSource = UICollectionViewDiffableDataSource<Int, String>
    typealias Snapshot = NSDiffableDataSourceSnapshot<Int, String>
    
    private var dataSource: DataSource!
    
    init() {
        let configuration =
        UICollectionLayoutListConfiguration(appearance: .grouped)
        let layout = UICollectionViewCompositionalLayout
            .list(using: configuration)
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // register cells
        let cellRegistration =
        UICollectionView.CellRegistration<
            UICollectionViewListCell, String
        > { cell, indexPath, text in
            var contentConfiguration = MyCellContentConfiguration()
            contentConfiguration.text = text
            cell.contentConfiguration = contentConfiguration
        }
        // configure datasource
        dataSource = DataSource(collectionView: collectionView) {
            collectionView, indexPath, itemIdentifier in
            collectionView.dequeueConfiguredReusableCell(
                using: cellRegistration,
                for: indexPath,
                item: itemIdentifier) }
        // configure snapshot
        var snapshot = Snapshot()
        let items = ["Ayo", "what da dog doin?"]
        let sections = [items]
        let sectionKeys = [Int](0..<sections.count)
        snapshot.appendSections(sectionKeys)
        sectionKeys.forEach {
            snapshot.appendItems(
                sections[$0],
                toSection: $0) }
        // inject snapshot & datasource
        dataSource.apply(snapshot)
        collectionView.dataSource = dataSource
    }
}

I think you'll have to use custom UIContentView and UIContentConfiguration objects to accomplish what you're talking about.

that being said, here's a simplified example of a content config/view/collection-list -

// MARK: UIContentConfiguration

struct MyCellContentConfiguration: UIContentConfiguration {
    var text: String?
    
    func makeContentView() -> UIView & UIContentView {
        MyCellContent(configuration: self)
    }
    
    func updated(
        for state: UIConfigurationState
    ) -> MyCellContentConfiguration {
        self
    }
}

// MARK: UIContentView

class MyCellContent: UIView, UIContentView {
    var configuration: UIContentConfiguration {
        get { cellConfiguration }
        set { configureCell() }
    }
    private var cellConfiguration: MyCellContentConfiguration
    private weak var label: UILabel!
    
    init(configuration: MyCellContentConfiguration) {
        self.cellConfiguration = configuration
        super.init(frame: .zero)
        addLabel()
        configureCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addLabel() {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        addSubview(label)
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
            label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            label.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
        ])
        self.label = label
    }
    
    private func configureCell() {
        label.text = cellConfiguration.text
    }
}

// MARK: UICollectionViewController

class MyCollectionViewController: UICollectionViewController {
    typealias DataSource = UICollectionViewDiffableDataSource<Int, String>
    typealias Snapshot = NSDiffableDataSourceSnapshot<Int, String>
    
    private var dataSource: DataSource!
    
    init() {
        let configuration =
        UICollectionLayoutListConfiguration(appearance: .grouped)
        let layout = UICollectionViewCompositionalLayout
            .list(using: configuration)
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // register cells
        let cellRegistration =
        UICollectionView.CellRegistration<
            UICollectionViewListCell, String
        > { cell, indexPath, text in
            var contentConfiguration = MyCellContentConfiguration()
            contentConfiguration.text = text
            cell.contentConfiguration = contentConfiguration
        }
        // configure datasource
        dataSource = DataSource(collectionView: collectionView) {
            collectionView, indexPath, itemIdentifier in
            collectionView.dequeueConfiguredReusableCell(
                using: cellRegistration,
                for: indexPath,
                item: itemIdentifier) }
        // configure snapshot
        var snapshot = Snapshot()
        let items = ["Ayo", "what da dog doin?"]
        let sections = [items]
        let sectionKeys = [Int](0..<sections.count)
        snapshot.appendSections(sectionKeys)
        sectionKeys.forEach {
            snapshot.appendItems(
                sections[$0],
                toSection: $0) }
        // inject snapshot & datasource
        dataSource.apply(snapshot)
        collectionView.dataSource = dataSource
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文