[英]How to Use NSTableViewDiffableDataSource to Load Data with NSTableView
I'm trying to learn how to use NSTableViewDiffableDataSource
to load data with NSTableView
.我正在尝试学习如何使用
NSTableViewDiffableDataSource
通过NSTableView
加载数据。 I am able to use UITableViewDiffableDataSource
and UICollectionViewDiffableDataSource
to load data in iOS because I have found some examples online.我可以使用
UITableViewDiffableDataSource
和UICollectionViewDiffableDataSource
在 iOS 中加载数据,因为我在网上找到了一些示例。 But I am not able to use NSTableViewDiffableDataSource
in Cocoa.但我无法在 Cocoa 中使用
NSTableViewDiffableDataSource
。
In the following case, I have a subclass of NSTableCellView
named TestTableCellView , which shows three fields: First name, Last name, and his or her date of birth in String.在下面的例子中,我有一个名为TestTableCellView的
NSTableCellView
子类,它显示了三个字段:名字、姓氏和他或她的出生日期(以字符串表示)。
import Cocoa
class ViewController: NSViewController {
// MARK: - Variables
var dataSource: NSTableViewDiffableDataSource<Int, Contact>?
// MARK: - IBOutlet
@IBOutlet weak var tableView: NSTableView!
// MARK: - Life cycle
override func viewWillAppear() {
super.viewWillAppear()
let model1 = Contact(id: 1, firstName: "Christopher", lastName: "Wilson", dateOfBirth: "06-02-2001")
let model2 = Contact(id: 2, firstName: "Jen", lastName: "Psaki", dateOfBirth: "08-25-1995")
let model3 = Contact(id: 3, firstName: "Pete", lastName: "Marovich", dateOfBirth: "12-12-2012")
let model4 = Contact(id: 4, firstName: "Deborah", lastName: "Mynatt", dateOfBirth: "11-08-1999")
let model5 = Contact(id: 5, firstName: "Christof", lastName: "Kreb", dateOfBirth: "01-01-2001")
let models = [model1, model2, model3, model4, model5]
dataSource = NSTableViewDiffableDataSource(tableView: tableView, cellProvider: { tableView, tableColumn, row, identifier in
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: self) as! TestTableCellView
let model = models[row]
cell.firstField.stringValue = model.firstName
cell.lastField.stringValue = model.lastName
cell.dobField.stringValue = model.dateOfBirth
return cell
})
tableView.dataSource = dataSource
guard let dataSource = self.dataSource else {
return
}
var snapshot = dataSource.snapshot()
snapshot.appendSections([0])
snapshot.appendItems(models, toSection: 0)
dataSource.apply(snapshot, animatingDifferences: true, completion: nil) // <--- crashing...
}
}
struct Contact: Hashable {
var id: Int
var firstName: String
var lastName: String
var dateOfBirth: String
}
Hmm... The application crashes with an error " Invalid parameter not satisfying: snapshot. " A couple of days ago, I tested another example, which also crashed at the same line (dataSource.apply).嗯......应用程序崩溃并出现错误“无效参数不令人满意:快照。 ”几天前,我测试了另一个示例,它也在同一行(dataSource.apply)崩溃。 I don't find many examples involving
NSTableViewDiffableDataSource
online.我在网上找不到很多涉及
NSTableViewDiffableDataSource
的示例。 The only example I have found is t his topic , which doesn't help.我发现的唯一例子是他的主题,这没有帮助。 Anyway, what am I doing wrong?
无论如何,我做错了什么? My Xcode version is 13.1.
我的 Xcode 版本是 13.1。 Thanks.
谢谢。
Create a snapshot like this and it should work:创建这样的快照,它应该可以工作:
guard let dataSource = self.dataSource else {
return
}
var snapshot = NSDiffableDataSourceSnapshot<Int, Contact>()
snapshot.appendSections([0])
snapshot.appendItems(models, toSection: 0)
dataSource.apply(snapshot, animatingDifferences: false)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.