获取NSTableView单元格值

问题描述 投票:0回答:2

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

  }
swift macos nstableview nstableviewcell
2个回答
0
投票

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

概括地说,我有一个内存数组。该数组已加载到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)")
    }
}

-1
投票

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

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)")
}
© www.soinside.com 2019 - 2024. All rights reserved.