打印NSTableView的用户单击行的行号

Tom*_*mer 11 macos cocoa nstableview nstableviewcell swift

我有NSTableView一个专栏.我想打印用户点击的行的行号.我不知道我应该从哪里开始.有这个方法吗?

aya*_*aio 16

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

在此示例中,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)
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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)
}
Run Code Online (Sandbox Code Playgroud)