11

我有NSTableView一列。我想打印用户单击的行的行号。我不确定我应该从哪里开始。有办法吗?

4

2 回答 2

16

您可以在 NSTableView 委托selectedRowIndexes的方法中使用 tableView 中的属性。tableViewSelectionDidChange

在这个例子中,tableView 允许多选。

斯威夫特 3

func tableViewSelectionDidChange(_ notification: Notification) {
    if let myTable = notification.object as? NSTableView {
        // we create an [Int] array from the index set
        let selected = myTable.selectedRowIndexes.map { Int($0) }
        print(selected)
    }
}

斯威夫特 2

func tableViewSelectionDidChange(notification: NSNotification) {
    var mySelectedRows = [Int]()
    let myTableViewFromNotification = notification.object as! NSTableView
    let indexes = myTableViewFromNotification.selectedRowIndexes
    // we iterate over the indexes using `.indexGreaterThanIndex`
    var index = indexes.firstIndex
    while index != NSNotFound {
        mySelectedRows.append(index)
        index = indexes.indexGreaterThanIndex(index)
    }
    print(mySelectedRows)
}
于 2015-03-22T21:27:52.060 回答
0

利用-selectedRowIndexes

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/#//apple_ref/occ/instp/NSTableView/selectedRowIndexes

然后您可以使用这些索引从您的dataSource (通常是数组)中获取数据

于 2015-03-22T18:42:40.020 回答