高级 NSCollectionView 和 NSTableView

一个用于 NSCollectionView、NSTableView 和 NSOutlineView 的框架。它提供了一系列类和扩展,其中许多是缺失的 UIKit API 的移植。

请查看包含的示例应用程序,它演示了大多数功能。

有关完整的文档,请查看 在线文档

NSCollectionView ItemRegistration & NSTableView CellRegistration

一个用于集合视图项目和表格单元格的注册,它极大地简化了它们的配置。 是 `UICollectionView.CellRegistration` 的移植。

struct GalleryItem {
    let title: String
    let image: NSImage
}

let tableCellRegistration = NSTableView.CellRegistration<NSTableCellView, GalleryItem> { 
    tableCell, column, row, galleryItem in
    
    tableCell.textField.stringValue = galleryItem.title
    tableCell.imageView.image = galleryItem.image
    
    // Gets called whenever the state of the item changes (e.g. on selection)
    tableCell.configurationUpdateHandler = { tableCell, state in
        // Updates the text color based on selection state.
        tableCell.textField.textColor = state.isSelected ? .controlAccentColor : .labelColor
    }
}

NSContentConfiguration

配置内容视图的样式和内容。 是 `UIContentConfiguration` 的移植。

NSCollectionviewItemNSTableCellViewNSTableRowView 提供 contentConfiguration,您可以在其中应用它们来配置项目/单元格的内容。

NSHostingConfiguration

一种适用于托管 SwiftUI 视图层次结构的内容配置。

使用此配置,您可以轻松地在集合项目和表格单元格中显示 SwiftUI 视图

collectionViewItem.contentConfiguration = NSHostingConfiguration {
    HStack {
        Image(systemName: "star").foregroundStyle(.purple)
        Text("Favorites")
        Spacer()
    }
}

NSListContentConfiguration

表格视图单元格的内容配置。

NSListContentConfiguration

var content = tableCell.defaultContentConfiguration()

// Configure content
content.text = "Text"
content.secondaryText = #"SecondaryText\\nImage displays a system image named "photo""#
content.image = NSImage(systemSymbolName: "photo")

// Customize appearance
content.textProperties.font = .body
content.imageProperties.tintColor = .controlAccentColor

tableCell.contentConfiguration = content

NSItemContentconfiguration

集合视图项目的内容配置。

NSItemContentconfiguration

public var content = collectionViewItem.defaultContentConfiguration()

// Configure content
content.text = "Text"
content.secondaryText = "SecondaryText"
content.image = NSImage(systemSymbolName: "Astronaut Cat")

// Customize appearance
content.secondaryTextProperties.font = .callout

collectionViewItem.contentConfiguration = content

NSCollectionView 重配置项目

更新项目的数据,而无需重新加载和替换它们。 与重新加载项目相比,它提供了更好的性能。 是 `UICollectionView.reconfigureItems` 的移植。

任何已通过 ItemRegistration 注册的项目,或者通过使用 register(_ itemClass: NSCollectionViewItem.Type) 按类注册的项目,都可以被重新配置。

collectionView.reconfigureItems(at: indexPaths)

NSTableView 通过类注册单元格

Apple 仅允许使用 NSNib 注册 NSTableCellView。 此框架允许您注册表格单元格类。

tableView.register(NSTableCellView.self)

let dequeuedTableCell = tableView.makeView(for: NSTableCellView.self)

NSCollectionView- & NSTableViewDiffableDataSource 项目删除

通过 DeletingHandlers 启用通过退格键删除项目

// Allow every item to be deleted
dataSource.deletingHandlers.canDelete = { items in return true }

// Update the backing store from the final item identifiers
dataSource.deletingHandlers.didDelete = { [weak self] items, transaction in
    guard let self = self else { return }
         
    self.backingStore = transaction.finalSnapshot.itemIdentifiers
}

NSDiffableDataSourceSnapshot 应用选项

Apple 的 apply(_:animatingDifferences:completion:) 提供了两个选项,用于根据 animatingDifferences 将快照应用于可区分的数据源

NSDiffableDataSourceSnapshotApplyOption 允许您即使在没有动画的情况下也执行差异比较,与使用 Apple 的 reloadData() 相比,性能更高。

它还提供其他选项

diffableDataSource.apply(mySnapshot, .withoutAnimation)

diffableDataSource.apply(mySnapshot, .animated(3.0))

CollectionViewDiffableDataSource

一个扩展的 NSCollectionViewDiffableDataSource,提供

它包括以下处理程序:

TableViewDiffableDataSource

类似于 CollectionViewDiffableDataSource。

OutlineViewDiffableDataSource

一个用于 NSOutlineView 的可区分数据源。

使用 String 作为 ItemIdentifierType 的数据源的示例用法

   let dataSource = OutlineViewDiffableDataSource<String>(outlineView: outlineView, cellRegistration: cellRegistration)
   
   var snapshot = OutlineViewDiffableDataSourceSnapshot<String>()
   let rootItems ["Root 1", "Root 2", "Root 3", "Root 4", "Root 5"]
   snapshot.append(rootItems)
   
   rootItems.forEach { rootItem in
       let childItems = (1...5).map { "\(rootItem).\($0)" }
       snapshot.append(childItems, to: rootItem)
       
       childItems.forEach { childItem in
           let grandchildItems = (1...5).map { "\(childItem).\($0)" }
           snapshot.append(grandchildItems, to: childItem)
       }
   }
   dataSource.apply(snapshot)

NSTableView 和 NSCollectionView 的快速查看

NSCollectionView/NSTableView isQuicklookPreviewable 启用通过空格键快速查看选定的项目/单元格。

有几种方法可以提供快速查看预览(有关更详细的文档,请参阅 FZQuicklook

struct GalleryItem: QuicklookPreviewable {
    let title: String
    let imageURL: URL
    
    // The file url for the quicklook preview.
    let previewItemURL: URL? {
        return imageURL
    }
    
    // The quicklook preview title displayed on the top of the Quicklook panel.
    let previewItemTitle: String? {
        return title
    }
}

let itemRegistration = NSCollectionView.ItemRegistration<NSCollectionViewItem, GalleryItem>() { collectionItem, indexPath, galleryItem in 
    // configurate …
}
  
collectionView.dataSource = NSCollectionViewDiffableDataSource<Section, GalleryItem>(collectionView: collectionView, itemRegistration: ItemRegistration)

collectionView.isQuicklookPreviewable = true
collectionView.quicklookSelectedItems()
collectionViewItem.quicklookPreview = URL(fileURLWithPath: "someFile.png")

安装

AdvancedCollectionTableView 添加到您的应用程序的 Package.swift 文件,或者在 Xcode 中选择“File -> Add Package Dependencies

.package(url: "https://github.com/flocked/AdvancedCollectionTableView")

如果您克隆了 repo,则可以运行示例应用程序,该应用程序演示了大多数 API。