繁体   English   中英

获取 NSTableView 单元格值

[英]get NSTableView cell values

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

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

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

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)")

  }

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

简而言之,我有一个内存数组。
该数组已加载到 tableView 中。 这是获取用户已取消的行并对这些行执行某些操作的代码。 这里我只是将它打印到控制台。

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)")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM