我想學習如何使用NSTableViewDiffableDataSource與加載資料NSTableView。我能夠在 iOS 中使用UITableViewDiffableDataSource和UICollectionViewDiffableDataSource加載資料,因為我在網上找到了一些示例。但我不能NSTableViewDiffableDataSource在可可中使用。
在下面的例子中,我有一個NSTableCellView名為TestTableCellView的子類,它以字串形式顯示三個欄位:名字、姓氏和他或她的出生日期。
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
}
嗯...應用程式崩潰并出現錯誤“無效引數不滿足:快照。 ”幾天前,我測驗了另一個示例,它也在同一行(dataSource.apply)崩潰。我在NSTableViewDiffableDataSource網上找不到很多例子。我發現的唯一例子是 t 
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/376245.html
標籤:迅速 苹果系统 稳定视图 可区分的数据源 nsdiffabledatasourcesnapshot
上一篇:如何清除在目標埠上運行的行程?
