在 NSTableView 末尾添加行

Add row at the end of NSTableView

我想在我的 NSTableView 的末尾添加一个新行。

我试过这段代码:

@IBOutlet weak var tblPositions: NSTableView!
let values = ["Test 1","Test 2","Test 3","Test 4","Test 5"]

func numberOfRows(in tableView: NSTableView) -> Int {
   return values.count
}

@IBAction func addRow(_ sender: Any) {
   tblPositions.beginUpdates()
   tblPositions.insertRows(at: IndexSet(integer: 0), withAnimation: .effectFade)
   tblPositions.endUpdates()
}

这段代码工作正常。但这总是在顶部而不是末尾添加一行。

试试这个:

@IBOutlet weak var tblPositions: NSTableView!
let values = ["Test 1","Test 2","Test 3","Test 4","Test 5"]

func numberOfRows(in tableView: NSTableView) -> Int {
   return values.count
}

@IBAction func addRow(_ sender: Any) {

    tableView.beginUpdates()
    tableView.insertRows(at: IndexSet(integer: values.count - 1), withAnimation: .effectFade)
    tableView.endUpdates()
}