2015-08-13 70 views
1

如何獲得行的前面選擇的指數NSTableViewNSTableView的舊選擇

方法

func tableViewSelectionIsChanging(notification: NSNotification) { 
    let index = (notification.object as! NSTableView).selectedRow 
    println(index) 
} 

func tableViewSelectionDidChange(notification: NSNotification) 
    let index = (notification.object as! NSTableView).selectedRow 
    println(index) 
} 

兩種方法打印相同的值。如何獲得因選擇而發生變化的索引/行?


或者簡單來說,如何讓我喜歡

println("\(Selection changed from \(tableView.oldSelectedIndex) to \(tableView.newSelectionIndex)") 

回答

2

得到它選擇了一個小區之前,該函數func tableView(tableView: NSTableView, shouldSelectRow row: Int)是呼籲delegate。在這裏,您可以看到當前選定的行和建議的選擇。

func tableView(tableView: NSTableView, shouldSelectRow row: Int) -> Bool { 

    // This is the next index 
    NSLog("Next index: \(row)") 

    // Make sure that a row is currently selected! 
    guard self.tableView.selectedRowIndexes.count > 0 else { 
     return true 
    } 

    // Get the currently selected row. 
    let index = self.tableView.selectedRow 
    NSLog("Previous index: \(index)") 

    return true 
} 

注意:這不正是回答你的問題,因爲你已經要求新的選擇後的變化發生,而這之前給它。但我希望這已經足夠了。

+0

由於不調用tableView(tableView:NSTableView,shouldSelectRow row:Int)'當用戶取消選擇一行時,此技術將不起作用。相反,使用委託方法'tableView(_ tableView:NSTableView,selectionIndexesForProposedSelection proposedSelectionIndexes:IndexSet) - > IndexSet'。 –

0

一份聲明中您可以通過

tableView.selectedRow 
+0

此答案不適用於多項選擇,更重要的是,此答​​案在選擇更改後返回選擇,而不是BFORE。該問題具體要求在改變之前進行選擇。 –