获取 NSTableView 单元格值



我是OSX编码和编码mac OS应用程序的新手。

如何访问用户所选行中的文本。 在这种特殊情况下,我允许用户一次性选择要从数据库中删除的多行,这就是为什么我需要检索我在数据库中用作键的文本。

在控制台屏幕中工作,这是我要做的简化版本。

import Cocoa
import Foundation
extension MainViewController {
func tableViewSelectionDidChange(_ notification: Notification) {
let myColumn = 0                          // -- first column
let myRow = tableViewFolders.selectedRow  // -- row int index clicked
let myCell:String = "?"  // -- ?get row or cell as string and/or array
print("myColumn: (myColumn)")
print("myRow: (myRow)")
print("myCell: (myCell)")
}

这是我能够弄清楚的最简单的答案形式。 这只是一列,但我认为它没有理由不适用于多列表。 检索行后,您必须使用列索引访问每个列元素。

简而言之,我有一个内存中数组。
该数组已加载到表视图中。 下面是获取用户已激活的行并对这些行执行某些操作的代码。 在这里,我只是将其打印到控制台。

import Cocoa
import Foundation
extension MainViewController {
// -----------------------------------------------------------
// -- Note: that myDataArray is loaded and in memory
// -- that data is now showing inside of the tableView
// -- now we want to do something with the rows the user
// -- has selected.
// -- also note: I'm only accessing a single column tableView
// -----------------------------------------------------------
func tableViewSelectionDidChange(_ notification: Notification) {
// -----------------------------------------------------------
// -- this is an array to store the selected rows data
// -- in my case this is actually an instance array
// -- in which I call .removeAll() on
// -----------------------------------------------------------
selectedRowsData[String] = []

// -----------------------------------------------------------
// -- get the set of indexes for the rows that are selected
// -----------------------------------------------------------
// -- myTableView.selectedRowIndexes 
// --    this is an index set containing 
// --    the indexes of the selected rows
let selectedIndexSet = myTableView.selectedRowIndexes     

// -----------------------------------------------------------
// -- if your curious this is the quantity of rows selected
// -- we will be looping through this set in a moment
// -- selectedIndexSet returns '# indexes' literally
// -- this index set contains something like ["2", "5", "9"]
// -- etc...
// -----------------------------------------------------------
// print("selectedIndexSet: There are (selectedIndexSet)")

// -----------------------------------------------------------
// -- loop through the index set and extract 
// -- from the original data array "myDataArray"
// -- the value that each row index points to
// -----------------------------------------------------------
for index in selectedIndexSet {  
if index > -1 {
// -----------------------------------------------------------
// -- append the actual data to selectedRowsData
// -- this will provide quoted string comma separated data 
// -- ["dataA", "dataB", "more data here"]
// -----------------------------------------------------------
selectedRowsData.append(myDataArray.self[index])
}
}
print("selectedRowsData n (selectedRowsData)")
}
}
//get the NsTableView cell from index in OSX / macOS catalyst 
let tableView = notification.object as! NSTableView
let row = table.selectedRow 
let column = table.tableColumnWithIdentifier("ColumnID")
// you can specified your column as well
guard  let cell  = tableView.view(atColumn: 0, row: selectedIndex, makeIfNecessary: true) as? YourCustomCell else { return }
// do whatever you want with your cell from selected Index 

要获取单行数据,您可以使用:

func tableViewSelectionDidChange(notification: NSNotification) {
let table = notification.object as! NSTableView
print(table.selectedRow);
var row = table.selectedRow
var column = table.tableColumnWithIdentifier("ID")
var cell: AnyObject? = column?.dataCellForRow(row)
print("Cell Value - (cell!.stringValue)")
}

最新更新