在NSTableView的末尾添加行

时间:2017-05-19 07:33:44

标签: swift macos 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()
}

此代码工作正常。但是这总是在顶部而不是结尾处添加我的行。

1 个答案:

答案 0 :(得分:2)

试试这个:

@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()
}
相关问题