如何在 macOS 上将 NSTableViewDiffableDataSource 用于具有多列和单元格的表格?

问题描述 投票:0回答:0

我是用于

NSTableView
s (macOS) 的 Diffable 数据源的新手。

此页面包含一个教程,其中显示的是

UITableViewDiffableDataSource
,我正在尝试将其适应macOS:

enum Section : Int , CaseIterable {
    case friendsContacts
    case allContacts
}

typealias DataSource = UITableViewDiffableDataSource<Section, Contact>
private lazy var dataSource = makeDataSource()
// ...
func makeDataSource() -> DataSource {
    let dataSource = DataSource(tableView: tableView, cellProvider: {( tableView, indexPath, contact) -> UITableViewCell? in

        let cell = tableView.dequeueReusableCell(withIdentifier: ContactTableCell.reuseIdentifier, for: indexPath) as? ContactTableCell
        cell?.configure(with: contact)
        return cell
    })
    return dataSource
}

我的问题是这样的。在 iOS 中,表格有部分,在 macOS 上,表格有列。

就我而言,我使用的是 3 列的表格。第一个是图像,另外两个是常规的 tabla 单元格,因为我只是使用它们的文本属性。我还没有为单元格创建任何类(应该吗?)。每列的每个单元格都有其标识符。

查看

makeDataSource()
方法,我们看到教程使用了一个名为
ContactTableCell
的类作为表格单元格,并且
reuseIdentifier
遵循该声明。

我的困难是,当我对包含不同单元格的 3 列表格使用

dequeueReusableCell
时,如何区分 macOS 上每列的单元格?

谢谢

macos cocoa nstableviewdiffabledatasource
© www.soinside.com 2019 - 2024. All rights reserved.