2015-03-22 17 views

risposta

14

È possibile utilizzare la proprietà selectedRowIndexes dal tableView nel tableViewSelectionDidChange metodo nel delegato NSTableView.

In questo esempio, tableView consente una selezione multipla.

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

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