2015-03-22 20 views

cevap

14

Sen tableViewSelectionDidChange yılında tableView dan selectedRowIndexes özelliğini kullanabileceğiniz dataSource (tipik olarak bir dizi) verileri kapmak için bu dizinleri kullanabilirsiniz NSTableView delegenizdeki yöntem.

Bu örnekte, tableView çoklu seçime izin verir.

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) 
} 
İlgili konular