一个用于 NSCollectionView、NSTableView 和 NSOutlineView 的框架。它提供了一系列类和扩展,其中许多是缺失的 UIKit API 的移植。
请查看包含的示例应用程序,它演示了大多数功能。
有关完整的文档,请查看 在线文档。
一个用于集合视图项目和表格单元格的注册,它极大地简化了它们的配置。 是 `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
}
}
配置内容视图的样式和内容。 是 `UIContentConfiguration` 的移植。
NSCollectionviewItem
、NSTableCellView
和 NSTableRowView
提供 contentConfiguration
,您可以在其中应用它们来配置项目/单元格的内容。
一种适用于托管 SwiftUI 视图层次结构的内容配置。
使用此配置,您可以轻松地在集合项目和表格单元格中显示 SwiftUI 视图
collectionViewItem.contentConfiguration = NSHostingConfiguration {
HStack {
Image(systemName: "star").foregroundStyle(.purple)
Text("Favorites")
Spacer()
}
}
表格视图单元格的内容配置。
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
集合视图项目的内容配置。
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
更新项目的数据,而无需重新加载和替换它们。 与重新加载项目相比,它提供了更好的性能。 是 `UICollectionView.reconfigureItems` 的移植。
任何已通过 ItemRegistration
注册的项目,或者通过使用 register(_ itemClass: NSCollectionViewItem.Type)
按类注册的项目,都可以被重新配置。
collectionView.reconfigureItems(at: indexPaths)
Apple 仅允许使用 NSNib
注册 NSTableCellView
。 此框架允许您注册表格单元格类。
tableView.register(NSTableCellView.self)
let dequeuedTableCell = tableView.makeView(for: NSTableCellView.self)
通过 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
}
Apple 的 apply(_:animatingDifferences:completion:)
提供了两个选项,用于根据 animatingDifferences
将快照应用于可区分的数据源
true
应用旧状态和新状态的差异,并在 UI 中动画更新。false
等同于调用 reloadData()
。 它会重新加载每个项目。NSDiffableDataSourceSnapshotApplyOption
允许您即使在没有动画的情况下也执行差异比较,与使用 Apple 的 reloadData()
相比,性能更高。
它还提供其他选项
diffableDataSource.apply(mySnapshot, .withoutAnimation)
diffableDataSource.apply(mySnapshot, .animated(3.0))
一个扩展的 NSCollectionViewDiffableDataSource
,提供
reorderingHandlers
来重新排序项目deletingHandlers
删除项目QuicklookPreviewable
的项目的项目快速查看预览它包括以下处理程序:
类似于 CollectionViewDiffableDataSource。
一个用于 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)
NSCollectionView/NSTableView isQuicklookPreviewable
启用通过空格键快速查看选定的项目/单元格。
有几种方法可以提供快速查看预览(有关更详细的文档,请参阅 FZQuicklook)
QuicklookPreviewable
的 ItemIdentifierType
的可区分集合视图和表格视图数据源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()
NSCollectionViewItems
和 NSTableCellView
的 quicklookPreview: QuicklookPreviewable?
属性collectionViewItem.quicklookPreview = URL(fileURLWithPath: "someFile.png")
将 AdvancedCollectionTableView
添加到您的应用程序的 Package.swift
文件,或者在 Xcode 中选择“File -> Add Package Dependencies
.package(url: "https://github.com/flocked/AdvancedCollectionTableView")
如果您克隆了 repo,则可以运行示例应用程序,该应用程序演示了大多数 API。