0

我想用@resultbuilder在 UIKit 中创建我自己的声明性 UICollectionView,类似于我们List {}在 SwiftUI 中得到的。

@resultBuilder创建的快照如下所示:

@resultBuilder
struct SnapshotBuilder {
    
    static func buildBlock(_ components: ListItemGroup...) -> [ListItem] {
        return components.flatMap { $0.items }
    }
    
    static func buildFinalResult(_ component: [ListItem]) -> NSDiffableDataSourceSectionSnapshot<ListItem> {
        var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<ListItem>()
        sectionSnapshot.append(component)
        return sectionSnapshot
    }
}

我还需要使用以下扩展来传递ListItemGroupSnapshotBuilder获取[ListItem]

struct ListItem: Hashable {
    
    let title: String
    let image: UIImage?
    var children: [ListItem]
    
    init(_ title: String, children: [ListItem] = []) {
        self.title = title
        self.image = UIImage(systemName: title)
        self.children = children
    }
}

protocol ListItemGroup {
    var items: [ListItem] { get }
}

extension Array: ListItemGroup where Element == ListItem {
    var items: [ListItem] { self }
}

extension ListItem: ListItemGroup {
    var items: [ListItem] { [self] }
}

我的List课看起来像这样:

final class List: UICollectionView {
    
    private enum Section {
        case main
    }
    
    private var data: UICollectionViewDiffableDataSource<Section, ListItem>!
    
    init(@SnapshotBuilder snapshot: () -> NSDiffableDataSourceSectionSnapshot<ListItem>) {
        super.init(frame: .zero, collectionViewLayout: List.createLayout())
        
        configureDataSource()
        data.apply(snapshot(), to: .main)
    }
    
    required init(coder: NSCoder) {
        super.init(coder: coder)!
    }
    
    private static func createLayout() -> UICollectionViewLayout {
        let layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        return UICollectionViewCompositionalLayout.list(using: layoutConfig)
    }
    
    private func configureDataSource() {
        let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, ListItem> {
            (cell, indexPath, item) in
            
            var content = cell.defaultContentConfiguration()
            content.image = item.image
            content.text = item.title
        }
        
        data = UICollectionViewDiffableDataSource<Section, ListItem>(collectionView: self) {
            (collectionView: UICollectionView, indexPath: IndexPath, identifier: ListItem) -> UICollectionViewCell? in
            
            let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
            cell.accessories = [.disclosureIndicator()]
            return cell
        }
    }
}

我正在使用List这样的

class DeclarativeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        
        let collectionView = List {
            ListItem("Cell 1")
            ListItem("Cell 2")
            ListItem("Cell 3")
            ListItem("paperplane.fill")
            ListItem("doc.text")
            ListItem("book.fill")
        }
        collectionView.frame = view.bounds
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(collectionView)
    }   
}

它似乎工作。我根据数据计数获取单元格行,但单元格是空的,如下所示:

在此处输入图像描述

我需要改变什么才能让它工作?我的错误在哪里?我可以在其中定义一个数据源UICollectionView吗?感谢您的回答。

4

1 回答 1

1

您忘记应用配置。

var content = cell.defaultContentConfiguration()
content.image = item.image
content.text = item.title
cell.contentConfiguration = content // this is important

既然UIListContentConfigurationstruct,当你这样做时,var content = cell.defaultContentConfiguration()你只会得到它的副本。

于 2021-11-05T14:23:31.193 回答