【问题标题】:get NSTableView cell values获取 NSTableView 单元格值
【发布时间】:2020-09-07 07:23:05
【问题描述】:

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

  }

【问题讨论】:

  • 密钥是否可编辑?
  • 如果我理解正确,没有。我只是将 select 语句中的数据显示到 tableView 中。当我单击行时,我将构建一个数组,以便稍后提供给数据库更新语句(将从数据库中删除这些记录),然后将数据库中的数据重新加载回表中。至少我是这么计划的。
  • 从数据模型或数据源而不是表视图中获取数据。
  • 我不明白。更改必须来自用户。用户可以看到数据的唯一地方是表中。我会继续寻找感谢您的尝试。
  • 表格如何知道要显示哪些数据?

标签: swift macos nstableview nstableviewcell


【解决方案1】:

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

简而言之,我有一个内存数组。
该数组已加载到 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)")
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我似乎无法在评论部分发布格式化的声明。所以我只想说,即使解决了错误,我也无法让上面的代码工作。输出为“零”。我是新手,所以我可能只是没有正确阅读和转换它。我正在使用 Catalina,Xcode 11.1
    • dataCellForRow 已弃用并用于基于单元格的表格视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2018-08-04
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多