2015-03-22 12 views

Trả lời

14

Bạn có thể sử dụng selectedRowIndexes tài sản từ tableView trong tableViewSelectionDidChange trong ủy nhiệm NSTableView của bạn.

Trong ví dụ này, tableView cho phép nhiều lựa chọn.

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) 
} 
Các vấn đề liên quan